Check if a number is submited with Javasript?

Discussion in 'JavaScript' started by hugocub, Sep 22, 2011.

  1. #1
    Is there anyway I can check to see if my user is submitting a number or not,? There is a textbox, and they are asked to enter a number into it, but I don't know how to verify if they entered one or not. Any help is welcome.
     
    hugocub, Sep 22, 2011 IP
  2. omarabid

    omarabid Well-Known Member

    Messages:
    1,509
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Odds are you don't know programming.
    Why do you need to check? If they submit the data, you should be able to get in the server side.
     
    omarabid, Sep 22, 2011 IP
  3. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #3
    on your form tag (<form>) you can add the onsubmit attribute..

    <form onsubmit="return validate(this);" ... > 
    HTML:
    then you can add this script..

    <script type="text/javascript">
    
    function validate(form)
    {
        var numfield = form.NumberFieldName.value;
        if(isNaN(numfield))
        {
          // add a notification here, say: alert("You entered an invalid number.");
          return false;
        }
        else
          return true;
    }
    
    </script>
    HTML:
     
    JohnnySchultz, Sep 23, 2011 IP
  4. hugocub

    hugocub Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I do know "programming"... But that doesn't mean I know every language. And I want to do it in javascript because the page that they are taken to after the forum is submitted redirects them to another site.
     
    hugocub, Sep 25, 2011 IP
  5. hugocub

    hugocub Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, I got it working with this tutorial I found.
     
    hugocub, Sep 25, 2011 IP
  6. SheetalCreation

    SheetalCreation Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #6
    Hugocub,
    One suggestion for you, if you are making use of user entered data on sever, you should not rely on only java script validation, because javascript can be disabled in browser.
    You should validate user entered data on server side as well. If you need any help in that i can help you.

    Sheetal
     
    SheetalCreation, Sep 27, 2011 IP
  7. hugocub

    hugocub Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I am doing that also. Thanks for the heads up.
     
    hugocub, Sep 29, 2011 IP
  8. developer.designer

    developer.designer Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    There is a function isNaN Which Stands for is Not a Number

    So You can check isNaN(document.getElementById("fieldid"))==false
     
    developer.designer, Oct 8, 2011 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    You could also just not allow anything but 0-9 in that textbox (using Javascript in an onKeyUp function) then test for the textbox being blank before submitting.
     
    Rukbat, Oct 8, 2011 IP