Validating Integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JPhilS
    New Member
    • Oct 2007
    • 2

    Validating Integer

    hi everyone....

    i have a problem in javascript as to validating integer before submiting it.
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by JPhilS
    hi everyone....

    i have a problem in javascript as to validating integer before submiting it.
    [code=html]
    <form name = "test_form" onsubmit = "return validate()">
    <input type = text name = "integer1">
    </form>
    [/code]

    [code=javascript]
    function validate()
    {
    if(document.tes t_form.integer1 .value /*validating condition*/) return true;
    else return false;
    }
    [/code]

    Kind regards,
    Dmjpro.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Do you want to allow only non-negative integers or any integer?

      For any integer, use parseInt() to parse. You can use isNaN() to check that the number is invalid.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        hi ...

        another alternative would be to use regEx for that, have a look at the following example, that matches all positve integers:

        [CODE=javascript]// var val will have true assigned in case of positive ints
        // otherwise it will be false
        // note: string_value is the value you want to test
        var val = /^\d+$/.test(string_va lue);[/CODE]
        kind regards

        Comment

        Working...