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 7 of 7

Thread: RegExp

  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post

    RegExp

    Hi all,

    I am having trouble understanding how to get RegExp to work. I've read so many tutorials but none have been clear. They all just explain the patterns you can check for but not how to piece them together! I am trying to write a RegExp pattern that checks an email address so that it allows the characters a-zA-z0-9_ before and after the @

    How would I go about doing this?
    I tried
    Code:
    new RegExp("[a-zA-z0-9_].+@[a-zA-z0-9_].+");
    but that didnt work.

    I appreciate any help

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    You can collapse a-zA-Z0-9_ into \w.

    Code:
    <script type = "text/javascript">
    var reg = /^\w+@\w+\.\w{2,6}$/;
    
    // OR var reg = new RegExp("^\\w+@\\w+\\.\\w{2,6}$"); // note in this syntax that the \ must be escaped with another \
    
    if (reg.test("me@mydomain.com")) {
    alert("foo!");
    }
    else {
    alert ("bar!")
    }
    </script>
    But that is not really a satisfactory email validation - it would pass 999@888.777. Try this:-

    Code:
    if (!(/^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([a-z0-9\-\.]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/i.test(email.value))) {   // email, no two consecutive dots, returns true if address is invalid.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 04-25-2010 at 06:53 PM. Reason: Corrected regex with ^ and $

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    I began by testing:
    Code:
    new RegExp("\\w+@\\w+");
    but I could still pass in @@@

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    new RegExp("\\w+@\\w+"); will pass multiple @ signs such as me@mydoma@in.com (but not me@@@mydomain.com) because the pattern is in fact matched by e@m (and indeed by a@i). However, the regexes I gave you are not quite correct as I forgot the ^ and $ signs (start and end of string, i.e. nothing in the string but the regex to match. ). I have amended Post #2 accordingly.

    var reg = new RegExp("^\\w+@\\w+$");
    will not pass @@@ (multiple @ signs)
    Last edited by Philip M; 04-25-2010 at 07:03 PM.

  • #5
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    new RegExp("\\w+@\\w+"); will pass multiple @ signs such as me@mydoma@in.com (but not me@@@mydomain.com) because the pattern is in fact matched by e@m (and indeed by a@i).
    I think I understand it, however, I changed my code to new RegExp("^\\w+@\\w+$"); and then tried entering @@@ and it still passed the test(...) function. I tried entering ;@; and even that didn't come out as false! I don't understand why my code isn't working. It only complains if there is no @ sign and nothing before and after it, ie. ".+@.+"

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Well, it works for me.

    Code:
    <script type = "text/javascript">
    var reg = new RegExp("^\\w+@\\w+$");
    if (reg.test("me@@@mydomain")) {  
    alert ("foo!");  // true
    }
    else {
    alert ("bar!");  // false
    }
    </script>
    Last edited by Philip M; 04-25-2010 at 09:00 PM.

  • #7
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Turns out it wasn't updating the JS file... Works now.


  •  

    Posting Permissions

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