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
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    validate 6 numeric digits??

    Hi guys!
    If I have a field called "number" how can I validate that the user enters a 6 digit long number and also validate against a user typing "000000"?
    Tracy

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by nikko50 View Post
    Hi guys!
    If I have a field called "number" how can I validate that the user enters a 6 digit long number and also validate against a user typing "000000"?
    Tracy
    Code:
    result = /^[1-9]\d{5}/.test(numbervalue);
    result is true for any number from 100000 to 999999.

    regards

  • #3
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts
    What if they are allowed to type in "011225". Number that start with 0.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by nikko50 View Post
    What if they are allowed to type in "011225". Number that start with 0.
    Code:
    val = parseInt(numval,10);
    result = (val > 0 && val <= 999999) ? true : false);
    from 1 to 999999.

    regards

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    if (/^\d{6}$/.test(number.value) && (number.value > 0)) {

    returns true for 000001 to 999999 (I understand that the user must enter exactly 6 digits with leading zeroes as necessary)

    If the six digit number must be at least 100000 then:-

    if (/^\d{6}$/.test(number.value) && (number.value >= 100000)) {

    or as proposed by oesxyl

    if (/^[1-9]\d{5}$/.test(number.value)) { // $ sign essential
    returns true for 100000 - 999999
    Last edited by Philip M; 04-21-2008 at 06:24 PM. Reason: Typo

  • Users who have thanked Philip M for this post:

    oesxyl (04-21-2008)


  •  

    Posting Permissions

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