Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: NaN

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    197
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NaN

    How can i detect if a substring represents a valid integer number

    my example is something like

    if (document.getElementById("user_response").value.substring(0,document.getElementById("user_response") .value.indexOf('.'))
    != NaN)

    is that sensible?
    thanks
    ez
    Animation Rule #64
    Poor quality images are often artistic,
    Poor quality sound is ALWAYS annoying.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Use a regular expression!

    teststring = thesubstringvalueyouwanttotest
    if (/\D/.test(teststring)) {
    do whatever is wanted if the test fails
    }

    \D means "match anything which is not a digit, i.e. not 0-9"

    So if a match is found the test fails (returns true here)

  • #3
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Further, you could always make use of the isNaN function.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    The questioner wished to detect whether a substring represents a valid integer number.

    The isNaN function would not reject a real (decimal) number.

    I assume that by "valid" the questioner means any integer number, as opposed to certain specific numbers which are considered to be "valid".

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    if(!(isNaN(parseInt(document.getElementById('user_response').value))))
    Hope that helps!

    Happy coding!

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    OK, that works as well if the user input is not a number ("xyz"), but if the user enters a real (decimal) number such as 3.14 then parseInt() returns 3, and the validation passes. I understood that the questioner wishes only INTEGER numbers to be entered into the field.

    With respect, I still prefer the regular expression!

    if(/\D/g.test(this.value)){alert('Only integer numbers are valid in this box. ');this.value='';this.focus()}
    Last edited by Philip M; 05-02-2004 at 09:20 AM.

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm...

    From his original code, it seems he is testing to see if the integer part of the value of the field is a number (he is taking the substring from the first character to the decimal place). Am I missing something?

    Happy coding!

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    So 123.XYZ is an acceptable or "valid" entry?

    Hmm....

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know - ez4ne12c didn't indicate what should follow the decimal point. He or she might require a special form in the form field.

    Happy coding!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •