Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    762

    JavaScript Regular Expression

    Hello,

    I am working on a simple form validation using javascript regexp.

    This is how i check if a "first name" field's value is valid by checking for only a-z , A-Z and space (this example is a cut-down version of my actual script):

    HTML Code:
    <html>
    <head>
    <title>Test</title>
    <script type="text/javascript" language="javascript">
    
    function checkFirstName()
    {
    	var regExp = /[^a-zA-Z ]/g;
    	var field = document.getElementByID("first_name");
    	
    	if (regExp.test(field.value))
    	{
    		alert("First Name field contains invalid charaters");
    	}
    }
    
    </script>
    </head>
    <body>
    <input id="first_name" type="text" name="first_name" onkeyup="checkFirstName();" />
    </body>
    </html>
    I'm i doing this right?

    what would be the right regexp for checking for a-z,A-Z,0-9,comma and space?

    i tried this -> var regExp = /[^a-zA-Z0-9, ]/g;

    on my actual script, instead of alert(); i change the field's text color to red if the words being typed has an error and green if it's correct.

    for some odd reason, it turns red and quickly turns back to green. any idea what's going on there?

  2. #2
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Your regex is at fault. Don't include the start metacharacter ( ^ ) within the character class. Try:

    ^[a-zA-Z, ]+

    Want to test regular expressions out on line?

    http://www.quanetic.com/Regex or here is a javascript specific version

    http://regexpal.com/
    Last edited by mwatkins; 07-29-2009 at 07:44 PM.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  3. #3
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    762
    how about this one? it works when i tested it but i dont know if it's correct.

    var regExp = /[^a-zA-Z,\s*]/;

    p.s. what was that + at the end of ] was for?

  4. #4
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Quote Originally Posted by latheesan View Post
    how about this one? it works when i tested it but i dont know if it's correct. var regExp = /[^a-zA-Z,\s*]/;
    Including the \s metacharacter in the character class makes sense if you want to match all sorts of whitespace (space, tabs...) rather than just the space character. I've no idea what you intend by including the '*' character.

    p.s. what was that + at the end of ] was for?
    [a-zA-Z\s,]+

    The + is key. You want to match one or more of the preceding element, and in this case the element is anything in the character class (between the []).

    There is still a problem with your regex - a string composed of nothing but whitespace and commas could still match or "pass". You'll want to enforce at least one letter at the start, I imagine. This may do what you wish:

    ^[a-zA-Z]+[a-zA-Z,\s]+

    Or short form:

    ^[A-z]+[A-z,\s]+
    Last edited by mwatkins; 07-29-2009 at 08:10 PM.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  5. #5
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Quote Originally Posted by mwatkins View Post
    Your regex is at fault. Don't include the start metacharacter ( ^ ) within the character class.
    Yes, that one's a good little trap for the unwary... Just to elaborate a bit for the OP (I suspect mwatkins knows this already) normally ^ means match at the start, but used at the beginning of a character class it negates the class. So:
    [a-z] = any lowercase letter
    [^a-z] = anything that isn't a lowercase letter
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  6. #6
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    foobic, thanks for adding that bit of context re negation; indeed when reading his original note I wondered if perhaps he wanted a string that didn't start with a lower case letter (perhaps he does) but the absence of a pattern repeat as well as the nature of the data (name) suggested to me that perhaps the OP just misplaced the start character.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  7. #7
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    762
    Thanks for the help guys, but either of these regexp aren't working right for me:

    ^[a-zA-Z]+[a-zA-Z,\s]+

    ^[A-z]+[A-z,\s]+

    as i type, the text color keeps changing from red to green.

    The first_name field is only allowed to have a-z , A-Z and Space (in any order and recursion is allowed).

    For example:

    Latheesan Kanes = OK
    Lathee$an Kanes = ERROR
    Latheesan Kane5 = ERROR

    what would be the right regexp for this?

  8. #8
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    The regexp looks right for what you're trying to do (except you could remove the comma, if you don't want to allow it). It seems like you have something else not working - can you show us an example page?
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  9. #9
    Join Date
    Apr 2005
    Posts
    1,711
    IMO, client-side regular expression is dangerous. If that's the only form of checking you have, be prepared to have major security problems. I would want to at least have backend validation in your form processor. Otherwise, this should work:

    var name = /^[^\W,\s]$/;
    if (name.test(field.value)) {
    alert("ono!");
    } else {
    alert("yipee!");
    }
    Zach E. - Kualowww.kualo.com
    Shared Web Hosting, Reseller Hosting, Cloud VPS & Dedicated Servers
    UK: 0800 138 3235 ❘ USA: 1-800-995-8256

Similar Threads

  1. regular expression help... again!
    By larwilliams in forum Programming Discussion
    Replies: 10
    Last Post: 06-03-2009, 01:10 AM
  2. Regular Expression - HeLP
    By helpwanted123 in forum Programming Discussion
    Replies: 2
    Last Post: 12-26-2006, 07:26 AM
  3. Regular Expression Help
    By mobilebadboy in forum Programming Discussion
    Replies: 3
    Last Post: 05-16-2005, 07:32 PM
  4. Using Regular Expression to...
    By NetVer in forum Programming Discussion
    Replies: 5
    Last Post: 12-30-2004, 08:16 AM
  5. Regular Expression
    By raulgonzalez in forum Programming Discussion
    Replies: 2
    Last Post: 09-12-2003, 06:51 PM

Posting Permissions

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