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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    can someone MOD this script for me?

    I have a script that checks for numeric values in a form field. The code is slightly over my head, but I want it to also accept the "-" symbol so that users can enter phone number or social security number but NOT letters... This is part of a much larger project so I can't use any other solutions... I need to MOD this exact code. Probably just changing one word or line, but I'm not having any luck... anyway, here is the code...

    /* The isNumeric function validates a string to determine whether or not it contains a numeric value.
    Parameters: lstr - Contains string value to validate
    Returns: true/false */
    function isNumeric(lstr)
    {
    lstr = rtrim(lstr);
    if (lstr != "")
    {
    //declare local variables
    var strlen, curptr, setptr, chk, inloop, decflag, minusflag, iserror;
    iserror = false;
    decflag = false;
    minusflag = false;
    strlen = lstr.length;
    curptr = 0;
    chk;
    // first check for space - .
    inloop = true;
    for (curptr = 0; curptr < strlen; curptr++)
    {
    chk = lstr.charAt(curptr);
    if (chk >= "0" && chk <= "9")
    {
    break;
    }

    if (chk == "-")
    {
    minusflag = true;
    break;
    }
    if (chk == ".")
    {
    decflag = true;
    break;
    }
    if (chk != " ")
    {
    return false;
    }
    }

    if (curptr >= strlen-1)
    {
    if (decflag || minusflag || chk == " ")
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    setptr = curptr + 1;
    for (curptr = setptr; curptr < strlen; curptr++)
    {
    chk = lstr.charAt(curptr);
    if (chk < "0" || chk > "9")
    {
    if (chk != ".")
    {
    return false;
    }
    else
    {
    if (decflag)
    {
    return false;
    }
    else
    {
    decflag = true;
    }
    }
    }
    }
    return true;
    }
    return false;
    }

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    that is some ugly code that a one lined regualr expression could do
    (In the future use code tags so it is easier to read)


    Try this:

    if (decflag || minusflag || chk == " ")

    change to

    if (decflag || chk == " ")

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    An awful lot of code which a regular expression would eat for breakfast.

    function isNumeric(lstr) {
    if (/[^0-9\s\-]/gi.test(lstr.value)) {
    // test fails
    }
    }


    In other words, the test fails if anything other than the digits 0-9, a space or a hyphen are entered into the field.

    "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
    Last edited by Philip M; 05-05-2004 at 08:19 PM.

  • #4
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    no luck

    Alien 51 - tried it... didn't work...bummer...


    Phillip - tried yours too... looked amazing. It allows letters to get through though??
    Last edited by wfcentral; 05-05-2004 at 07:10 PM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    No, if letters or characters such as $ % & ( ) are entered the test fails.

    If a character within the string to be tested is NOT 0-9 and NOT a space and NOT a hyphen the test fails. OK?

  • #6
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    maybe I'm not using it correctly

    Phillip -

    I replaced the entire function that I was using with your new example. I then went to the form and tested it by putting in "444-55-pppp" and it went through just fine... (meaning it did NOT tell me that I entered non-numeric data)

    Was I supposed to use your lines of code AND some code from my original post?

    sorry,
    Robert
    Last edited by wfcentral; 05-05-2004 at 07:33 PM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Sorry, there were a couple of typos.

    This works:-

    <SCRIPT = JavaScript>
    function isNumeric(lstr) {
    alert (lstr.value);
    if (/[^0-9\s\-]/gi.test(lstr.value)) {
    alert ("Test failed");
    return false;
    }
    alert ("test passed");
    return true;
    }
    </SCRIPT>
    Last edited by Philip M; 05-05-2004 at 08:18 PM.

  • #8
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getting better

    Phillip,

    that almost works... the test always fails now though... I can't get it to accept anything...

    Robert

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Robert -

    It is working for me.

    I entered 123 4-5 and it passed
    I entered 123ab and it failed.

    What does alert(lstr.value) show you (i.e. is the passed value correct?)


    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
    Last edited by Philip M; 05-05-2004 at 09:04 PM.

  • #10
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it says "undefined"

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Here is the whole thing I have been testing on:-

    <HTML>
    <HEAD>
    </HEAD>

    <BODY>

    <tr>
    <td><font face="Arial"><em><b>Enter some text here</b></em></font></td>
    <td><input type="text" size="25" maxlength="25" name="testtext" onblur="isNumeric(this)"> </font></td></tr>

    <SCRIPT = JavaScript>
    function isNumeric(lstr) {
    alert (lstr.value);
    if (/[^0-9\s\-]/gi.test(lstr.value)) {
    alert ("Test failed");
    return false;
    }
    alert ("test passed");
    return true;
    }

    </SCRIPT>

    </BODY>
    </HTML>

    If lstr.value is undefined it must be that the value is not being passed.

    Check your code for onblur="isNumeric(this)" (including spelling, case etc. as JavaScript is case sensitive and isnumeric is not the same as isNumeric.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    It occurs to me that a blank textbox will also pass, which is probably not what you want.

    So here is a mod to overcome that:-

    <SCRIPT = JavaScript>
    function isNumeric(lstr) {
    alert (lstr.value);
    if (/[^0-9\s\-]/gi.test(lstr.value) || (lstr.value.length==0)) {
    alert ("Test failed - invalid characters");
    return false;
    }
    }
    alert ("Test passed");
    return true;
    }

    </SCRIPT>

    You can of course then specify a minimum length for lstr (say 3 digits or whatever) by altering == 0 to <4 or whatever.
    Last edited by Philip M; 05-05-2004 at 09:32 PM.

  • #13
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    YEAH! it works

    Phillip - THANKS FOR ALL YOUR HELP

    I found the reference you were talking about in the old code and made some slight modifications to your code - all works GREAT now!

    Code:
    function isNumeric(lstr) {
    	lstr = rtrim(lstr);
    alert (lstr);
    if (/[^0-9\s\-]/gi.test(lstr)) {
    alert ("Test failed");
    return false;
    }
    alert ("test passed");
    return true;
    }
    - Robert

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Great Robert,

    But have a look at my post a few seconds before yours for an improvement!

    Glad to have helped. This is an excellent example of how a simple regular expression can replace great chunks of ugly code.

  • #15
    New Coder
    Join Date
    May 2003
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    one more thing

    hey Phillip,

    before you head off...

    what would I modify if I want people to be able to enter ' or "

    such as how tall are you? 5' 4"

    Thanks,
    Robert


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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