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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Slow RegEx issue

    Happy New Year all,

    I am running this function onkeydown from a text input. The function changes the color of the text depending on the results from this regex test:

    Code:
    return (content.match(new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/))) ? true : false;
    The longer my string gets, the longer it takes to do this RegEx. I have quite the powerful computer, but my browsers lags quite a bit when the string gets over 12 or so characters. (This regex tests an email address.)

    I am thinking that it might be faster to either have my server do the regex, and use AJAX to test the string, or to do a 200-400 milisecond timeout on this function, and clear the timeout everytime they hit a new key, (to try and limit the number of times this script runs, while still giving them near-live results.)

    So my question is, Is there a faster way to do good email address verification, or should I do this by AJAX, or should I use a timer to try and remove itterations of the function.

    If you think i should use a timer, cam you give me a working example of clearing the same timer from two functions. I always have issues getting a global variable to work when i want to set a timer in one function, and clear it in another.

    Thanks!
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    1. what about emails from .co.uk?

    2. about globals: don't use them, use object props pn the function itself:
    Code:
    function tes(a){ tes.data+=a; return tes.data }
    tes.data = "";
    
    alert(tes("hello"))
    alert(tes(" world"))

    also might glimpse at dustin's take for inspiration


    note that using "|" will slow down the regexp eval considerably, as it has to do a test for each case, inclusive of patterns to the left/right of the |.
    i would imagine that using a [\w]{2,6} would be faster.

    a trick i have used in situations like this , is to break the regexp into two parts.
    first match all the substrings that might remotly be construed as an email, like "\w+@\w+\.\w{2,6}", and then filter out precisely all of those matches. that gives you the best of both worlds: a fast gateway and a precise validation filter.
    Last edited by rnd me; 01-02-2008 at 05:10 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Thank you,

    First of all, I don't see how the code you posted has anything to do with my question. I'm assuming im being stupid tonight, and I was hoping you could explain.

    Also, could you place the regex sections you were taking about within the regex I posted so that I could see where you are takling about?

    Thanks for the help!
    If I'm postin here, I NEED YOUR HELP!!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The numerous OR | operators are the cause. Suggest you try:-

    /^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([a-z0-9\-\.]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/i

    which covers co.uk and so on.

    There is no point in trying to validate to excess, as the biggest risk is that the address as entered is simply wrong due to a typo, e.g. philip@mydomian.co.uk


    It is your responsibility to die() if necessary….. - PHP Manual

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    So my question is, Is there a faster way to do good email address verification, or should I do this by AJAX, or should I use a timer to try and remove itterations of the function.
    The easiest thing to do would be to validate the email address onsubmit instead of onkeydown. Eliminates the issue entirely.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by chump2877 View Post
    The easiest thing to do would be to validate the email address onsubmit instead of onkeydown. Eliminates the issue entirely.
    Or better still onchange or onblur to get more immediate validation.

    But the guy wants:- "The function changes the color of the text depending on the results from this regex test:"

    I suppose the idea is that if the user types a character not consistent with an email address such as @@ then it turns red or something. Too clever by half IMHO.

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    The easiest thing to do would be to validate the email address onsubmit
    Thats nearly pointless. At that point you should just do server side validation (which any good programmer is going to do anyway.) I am looking for (and already have, but am trying to speed up) a nice looking live validation that lets the user know when they have entered the correct information.

    On another note, thank you Philip, I will give that Regex a try. There are A LOT of Regex's out there for email validation, and its difficult to find one that covers the majority of email addresses without being too slow. (A regex that conforms to every rule of email address creation and domain names would be many many lines long.)
    If I'm postin here, I NEED YOUR HELP!!

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The simplest validation is to require the email address to be entered twice (i.e confirm) which gets rid of most typos.

    Another good regex is:-

    /^.+@[^\.].*\.[A-z]{2,6}$/

    Simple, but allows for foreign language characters.

    i raed a wlihe ago taht as lnog as the frsit and lsat lttesrs in a wrod are in the cerroct pcale msot of us wulod siltl be albe to raed bceause our barnis jsut looks at the frsit and lsat lttesrs.

  • #9
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    Thats nearly pointless. At that point you should just do server side validation (which any good programmer is going to do anyway.)
    It's hardly pointless...The point of client-side validation is to validate fields before you make another trip back to the server...However you choose to do your client side validation, that is the bottom line...

    I think Philip said it right: "Too clever by half IMHO."

    What's happening here is you're getting caught up in making the validation "pretty", but the more you "pretty-ify" it, the less functional your validation becomes.

    So, in my opinion, the validation on onkeydown that you are implementing here is a little over the top and doesn;t really enhance the effectiveness of the validation. Sometimes simple is better -- for the developer and the end user.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #10
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Regardless of all of this, I still need help on my regex, server side or client side.

    Philip:
    The regex that you gave me seems to have a few problems.

    Code:
    /^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([a-z0-9\-\.]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/i
    This regex returns true if the email address has no dot. For instance myEmail@address passes true. Can that be corrected?
    Last edited by Ultragames; 01-02-2008 at 10:14 PM.
    If I'm postin here, I NEED YOUR HELP!!

  • #11
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Looking at your original regex, this should make it faster:

    Code:
    var myFunc = (function() {
      var pattern = /^\w+(?:[.-]?\w+)\w+@\w+(?:[.-]?\w+)\w+\.(?:com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-z]{2})$/
      return function(args) {
        //other function code here
        return pattern.test("abc@hotmail.com");
      };
    })();
    All I did was rid the regex of capturing parenthesis, changed it to regex.test(str) instead of str.match(regex), and stored a copy of the regex as a "quasi-local" variable. No need of recompiling that regex every time .
    Last edited by Trinithis; 01-03-2008 at 02:38 AM. Reason: explanation & forgot to uncapture a group.
    Trinithis

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Ultragames View Post
    Regardless of all of this, I still need help on my regex, server side or client side.

    Philip:
    The regex that you gave me seems to have a few problems.

    Code:
    /^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([a-z0-9\-\.]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/i
    This regex returns true if the email address has no dot. For instance myEmail@address passes true. Can that be corrected?
    It returns false for me. Have you copied it correctly?

    (.[a-z]{2,6}) requires a dot followed by 2-6 alpha characters at the end of the string
    Last edited by Philip M; 01-03-2008 at 09:06 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
    •