how to validate numeric value by using Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kang jia
    New Member
    • Jun 2007
    • 88

    #1

    how to validate numeric value by using Javascript

    hi, now i want to validate numeric value which user filled in in text box, it can have 2 decimal places. what is the regular expression i can use to achieve this? anyone can help?

    can reply me ASAP, it is urgent. thanks in advance
  • menocomp
    New Member
    • Apr 2007
    • 5

    #2
    [CODE=javascript]function CheckIntegers(e )
    {
    var keynum
    var keychar
    var numcheck
    if(window.event ) // IE
    {
    keynum = e.keyCode
    }
    else if(e.which) // Netscape/Firefox/Opera
    {
    keynum = e.which
    }
    keychar = String.fromChar Code(keynum)
    numcheck = /\d/
    return numcheck.test(k eychar)
    }[/CODE]
    Last edited by gits; Jul 25 '07, 12:24 PM. Reason: added CODE tags

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      hi ...

      you may use a regEx but a simple way is to check the value you want with:

      [CODE=javascript]isNaN(value)[/CODE]

      that returns true in case value isN(ot)aN(umber ) ... otherwise it returns false ;)

      kind regards

      Comment

      • kang jia
        New Member
        • Jun 2007
        • 88

        #4
        Originally posted by menocomp
        [CODE=javascript]function CheckIntegers(e )
        {
        var keynum
        var keychar
        var numcheck
        if(window.event ) // IE
        {
        keynum = e.keyCode
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
        keynum = e.which
        }
        keychar = String.fromChar Code(keynum)
        numcheck = /\d/
        return numcheck.test(k eychar)
        }[/CODE]
        besides this long method, do you know any regular expression which can help to check number value and its decimal places( i only need 2 decimal places). thanks for it.

        Comment

        Working...