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 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validation help

    hi,

    This should be pretty straight forward (I know) but maybe my syntax is wrong or something???

    I am validating a textfield for a string with only integers or commas.

    This is what i have and it doesn't seem to work correctly.


    PHP Code:
    <script language="JavaScript">
    function 
    validateString(objformObjformValue) {
    var 
    regex = /^[0-9,]$/;
    return 
    regex.test(formValue);
    }
    </script> 
    [php]

    Fingers crossed it's something simple!

    thanks,

    jo
    Last edited by ojoonline; 12-26-2005 at 02:43 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    You need to escape the comma character

    var regex = /^[0-9\,]$/;

    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

  • #3
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi Phillip,

    thanks for your reply!

    I tested and tested this and it didn't seem to work correctly... then I realised that I was only testing for one number followed by a comma! eg. 6,

    So, now I have this:

    PHP Code:
    <script language="JavaScript">
    function 
    validateString(objformObjformValue) {
    var 
    regex = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
    return 
    regex.test(formValue);
    }
    </script> 
    which is fine for one 11 digit number. I need to also match a list of comma delimited 11 digit numbers which might give me something like this??

    var regex = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9\,]+$/;

    When I try that regex, I end up with mixed results:

    N: 2222
    N: 22222
    Y: 22222222222
    Y: 22222222222,333 (this shouldn't pass)
    Y: 22222222222,33333333333,4444 (this shouldn't pass)

    Any help would be greatly appreciated.

    thanks, jo

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    var regex = /^[0-9]{11}(\,[0-9]{11})*$/;
    dumpfi

  • #5
    New Coder
    Join Date
    Feb 2003
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi dumpfi,

    after I posted that code I started to "see" the regex string and how it had to deal with a single number string and then repeated ones too... when I saw the regex you so kindy wrote - I knew it was the right thing.

    Thank you, thank you, thank you!!

    jo


  •  

    Posting Permissions

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