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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2005
    Posts
    257
    Thanks
    8
    Thanked 0 Times in 0 Posts

    validate numbers

    How I Can validate only numbers using the next format?

    PHP Code:
    if (theForm.username.value == "")
      {
        
    alert("Please enter a value for the \"Username\" field.");
        
    theForm.username.focus();
        return (
    false);
      } 

  • #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
    if (/^\d{1,}$/.test(theField.value)) { /// match one or more digits and nothing but (if true test passes)

    or

    if (/\D/.test(theField.value)) { /// match any non-digit (if true test fails)

  • #3
    Regular Coder
    Join Date
    Aug 2005
    Posts
    257
    Thanks
    8
    Thanked 0 Times in 0 Posts
    what mean ".test"?

  • #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
    Quote Originally Posted by urgido
    what mean ".test"?
    It means what it says. Test the value contained in the field to identify whether it is true or false. In your case test that the field contains one or more digits but nothing else. If that returns true then the test passes.

    This is what is called a regular expression. For more information see

    http://www.javascriptkit.com/javatutors/re.shtml

  • #5
    Regular Coder
    Join Date
    Aug 2005
    Posts
    257
    Thanks
    8
    Thanked 0 Times in 0 Posts
    PHP Code:
    if (theForm.PostCode.value == "")  
      {
        
    alert("Please enter a value for the \"PostCode\" field.");
        } else if (/^
    d{1,}$/.test(PostCode.value)) {
        
    alert("Please enter only numbers for \"PostCode\" field.");
        
    theForm.PostCode.focus();
        return (
    false);
      } 
    Is this correct? Thank you for your help master

  • #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
    Quote Originally Posted by urgido
    PHP Code:
    if (theForm.PostCode.value == "")  
      {
        
    alert("Please enter a value for the \"PostCode\" field.");
        } else if (/^
    d{1,}$/.test(PostCode.value)) {
        
    alert("Please enter only numbers for \"PostCode\" field.");
        
    theForm.PostCode.focus();
        return (
    false);
      } 
    Is this correct? Thank you for your help master

    Yes, but
    else if (/^\d{1,}$/.test(PostCode.value))
    means that the test passes with just one numeral entered in the field.
    You may prefer say
    else if (/^\d{4,}$/.test(PostCode.value))
    which requires at least 4 digits (e.g.9999999) and nothing but
    or
    else if (/^\d{4}$/.test(PostCode.value))
    which requires exactly 4 digits.

    But be careful - if you insist on a postcode format applicable to one country the effect may be to block users from a different country with a different postcode format (involving letters as well as numbers).

    For what it is worth here is the script I use to format postcodes:-

    function fixcode(pcode) {
    pcode1 = pcode.value.toUpperCase();
    if (pcode1 == "") { // or possibly if (pcode1.length <4); // four chars minimum
    alert (Please enter a value for the PostCode field");
    theForm.pcode.focus();
    return false;
    }
    pcode1 = pcode1.replace (/[^(A-Z)(0-9)(\s)(\-)]/gi, ""); // Remove invalid characters
    if (/^\D/.test(pcode1)) { // if an alpha character at start of field
    pcode1 = pcode1.replace (/^(\w+)(\d)(\d)(\d)(\w+)$/gi, "$1$2$3 $4$5"); //format
    pcode1 = pcode1.replace (/^(\w+)(\d)(\d)(\w+)$/gi, "$1$2 $3$4");
    }
    pcode.value = pcode1;
    }
    Last edited by Philip M; 07-31-2006 at 08:32 AM.


  •  

    Posting Permissions

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