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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    very simple form validation quetsion

    Hi
    I am trying to valid a simple address, that works fine apart from the fact that it will not allow forward slash in it "/" such as: Unit 12/13 some where.

    In brief I want to change /^[\w ]+$/; to include a forward character in it.

    I am new to javascript.

    many thx.

    p.s. every time I google it they find me email address validation dehhhhh



    I use the following code:

    function validateVat_No(fld) {
    var error = "";
    var illegalChars = /^[\w ]+$/; // allow letters, numbers, and spaces

    if (fld.value !== "") {

    if (!illegalChars.test(fld.value)) {
    fld.style.background = 'Yellow';
    error = "The Vat No contains illegal characters.\n";
    }
    }

    else {
    fld.style.background = 'White';

    }
    return error;
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Code:
    var illegalChars = /^[\w\s\/]+$/; // allow letters, numbers, spaces and /
    Your "illegalChars" are in fact the characters which are allowed. And if it is supposed to validate a mailing address how does a VAT number come into it?
    Note that that regex will allow a valid entry of one or more spaces. You should strip all leading/trailing spaces with
    Code:
    fld.value = fld.value.replace(/^\s+|\s+$/g,"");
    You should also reject entries of less than a certain length - say 6 if it is supposed to be an address.
    Code:
    if (fld.value.length < 6) {
    error = "The address appears to be incomplete";
    }
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 12-14-2010 at 08:37 AM. Reason: Correct typoas below

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi
    That didn't work unfortunately.
    All I really want is to assign the variable to accept letters number space and "/" the things that one uses to enter the address in a form.

    The code I should have posted, is..
    Code:
    function validateAddress(fld) {
        var error = "";
      var illegalChars = /^[\w +, ]+$/; // allow letters, numbers, and spaces
     
        if (fld.value == "") {
            fld.style.background = 'Yellow'; 
            error = "You didn't enter an Address.\n";
        } else if ((fld.value.length < 1) || (fld.value.length > 80)) {
            fld.style.background = 'Yellow'; 
            error = "The Address is the wrong length.\n";
        } else if (!illegalChars.test(fld.value)) {
            fld.style.background = 'Yellow'; 
            error = "The Address contains illegal characters.\n";
        } else {
            fld.style.background = 'White';
        }
        return error;
    }
    Please feel free to suggest other characters that one may need to include in an address. eg. of address unit 12/13 some where.

    Thx for your help.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Sorry, there was a typo (although it still worked for the example). But
    var illegalChars = /^[\w +, ]+$/; // allow letters, numbers, and spaces
    is not the code I gave you. If you want to allow a + sign it must be escaped thus \+
    If you want to allow a comma it is best to escape that as well \,
    It might be a good idea to allow a hyphen \- and a full stop \. and an apostrophe \' (25-34, St.John's Street).

    Code:
    var address = "Unit 12/13, 25-34 St.John's Street"
    var illegalChars = /^[\w\s\/\-\.\,\']+$/; // allow letters, numbers, spaces, full stops, hyphens, commas and / 
    if (!illegalChars.test(address)) {
    alert ("There are illegal characters in the address")
    }
    else {
    alert ("Address is OK")
    }
    Alternatively you could of course simply strip out any illegal characters.

    Don't forget to strip leading and trailing spaces (essential!) , and possibly reduce multiple spaces to one single space.
    Last edited by Philip M; 12-14-2010 at 08:53 AM.

  • #5
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi
    My apologies, that worked fine. Hadn't tested it properly.
    However I have one small problem.
    I also test the data using PHP, just in case user has javascript turned off. I know this is not PHP forum but most coders also know about php coding.
    I wish to allow "/" and "-" in the php code but doesn't seem to work.

    original code:

    Code:
       function Address($Address)
    {
     if (!preg_match("/^[A-Za-z0-9]([A-Za-z0-9-,\s]*[A-Za-z0-9\s])*$/", $Address))
    	  
    	  
            return false;
                
          else 
                 return true;
                }
    altered code:
    Code:
      function Address($Address)
    {
     if (!preg_match("/^[A-Za-z0-9]([A-Za-z0-9-,\/\-\s]*[A-Za-z0-9\s])*$/", $Address))
    	  
    	  
            return false;
                
          else 
                 return true;
                }
    please help. Thx

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    You should post questions about PHP in the PHP forum.

    But within a character class ([A-Za-z0-9-, a hyphen must be escaped.

  • #7
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    That worked a treat

    Hi
    Many thanks for your help greatly appreciated.


    p.s. any special character in a Character Class, is no longer a special character

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by jacka1 View Post
    p.s. any special character in a Character Class, is no longer a special character
    The only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (\), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will still work fine if you escape the regular metacharacters inside a character class, but some consider that doing so reduces readability.


  •  

    Posting Permissions

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