doubledee Posted January 21, 2012 Share Posted January 21, 2012 The following Regex checks for a valid E-mail address - per my definition - and seems to be working quite nicely... if (preg_match('#^[A-Z0-9_\+-]+(\.[A-Z0-9_\+-]+)*@[A-Z0-9-]+(\.[A-Z0-9-]+)*\.([A-Z]{2,7})$#i', $trimmed['email'])){ Now I need to change it so that the E-mail can be no more than 60 characters in total length since that is the size of my database field. I am unsure how to change things. This would be my best guess, but I could use some expert help here... if (preg_match('#^([A-Z0-9_\+-]+(\.[A-Z0-9_\+-]+)*@[A-Z0-9-]+(\.[A-Z0-9-]+)*\.([A-Z]{2,7})){,60}$#i', $trimmed['email'])){ Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
ragax Posted January 21, 2012 Share Posted January 21, 2012 Hi Debbie, Try this. Without looking at the details of your expression, I inserted a lookahead at the very beginning. It checks that the string has between 1 and 60 characters. if (preg_match('#^(?=.{1,60}$)[A-Z0-9_\+-]+(\.[A-Z0-9_\+-]+)*@[A-Z0-9-]+(\.[A-Z0-9-]+)*\.([A-Z]{2,7})$#i', $trimmed['email'])){ It will match 123@5678901234567890123456789012345678901234567890123456.com but not 123@56789012345678901234567890123456789012345678901234567.com (One more digit before the .com) Quote Link to comment Share on other sites More sharing options...
ragax Posted January 21, 2012 Share Posted January 21, 2012 P.S.: It's the same principle as for your strong password thread. Hope it works for you, let me know if you run into any probs. Wishing you a fun weekend, Andy Quote Link to comment Share on other sites More sharing options...
doubledee Posted January 21, 2012 Author Share Posted January 21, 2012 Hi Debbie, Try this. Without looking at the details of your expression, I inserted a lookahead at the very beginning. It checks that the string has between 1 and 60 characters. if (preg_match('#^(?=.{1,60}$)[A-Z0-9_\+-]+(\.[A-Z0-9_\+-]+)*@[A-Z0-9-]+(\.[A-Z0-9-]+)*\.([A-Z]{2,7})$#i', $trimmed['email'])){ It will match 123@5678901234567890123456789012345678901234567890123456.com but not 123@56789012345678901234567890123456789012345678901234567.com (One more digit before the .com) You and your lookaheads, playful?! A few things... 1.) Would my code also accomplish the same thing? 2.) Looking at my Regex, an e-mail can't be shorter than 6 characters, right? 3.) I hope to be ready to pick up our lookahead conversation sometime soon... Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
ragax Posted January 21, 2012 Share Posted January 21, 2012 Would my code also accomplish the same thing? No, your {,60} quantifier applies to the whole expression, so it would allow up to 60 email addresses. Looking at my Regex, an e-mail can't be shorter than 6 characters, right? Correct. There's no need to bother about the lower boundary of the quantifier. (As you already knew, seeing your quantifier.) I hope to be ready to pick up our lookahead conversation sometime soon... Fabulous. Good to hear your voice, Debbie, talk to you soon. -A Quote Link to comment Share on other sites More sharing options...
doubledee Posted January 21, 2012 Author Share Posted January 21, 2012 Would my code also accomplish the same thing? No, your {,60} quantifier applies to the whole expression, so it would allow up to 60 email addresses. Just to humor me, how would I fix my original code to limit e-mails to 60 characters and NOT use a lookahead? I tried using this as a guide... if (preg_match('#^[A-Z \'.-]{2,20}$#i', $trimmed['firstName'])){ Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
ragax Posted January 21, 2012 Share Posted January 21, 2012 Just to humor me, how would I fix my original code to limit e-mails to 60 characters and NOT use a lookahead? Mmm... 1. You could limit the size of each component (e.g., the name) with a quantifier such as {2,10}. Not a solution that would impress Bill Gates. 2. You could write a horrible OR tree to specify each of the characters (if you had 200 years to live). 3. You could use a strlen to check the input programmatically. 4. And... your favorite, I am sure: just before the $, you could insert a (?<=^.{1,60}), which is a lookbehind. But not in PHP, as it doesn't allow variable-width lookbehinds (.NET does) I'll post more if they come to mind. Warmest wishes, A Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 before the @ character you could do {1,60} this should limit the first part of the email to 60 characters no???? and there is no workaround without using a lookahead or behind playful is correct about that. Quote Link to comment Share on other sites More sharing options...
doubledee Posted January 21, 2012 Author Share Posted January 21, 2012 before the @ character you could do {1,60} this should limit the first part of the email to 60 characters no???? and there is no workaround without using a lookahead or behind playful is correct about that. Guess I'll just have to "suck it up" and try to join the ranks of the "real programmers"... Debbie Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted January 21, 2012 Share Posted January 21, 2012 JavaScript function used for validation and character length. function validate_email(email) { var regex=#^[A-Z0-9_\+-]+(\.[A-Z0-9_\+-]+)*@[A-Z0-9-]+(\.[A-Z0-9-]+)*\.([A-Z]{2,7})$#i; return email.length <= 60 && regex.test(email); } will return the pattern with a 60 character email. Quote Link to comment Share on other sites More sharing options...
ragax Posted January 21, 2012 Share Posted January 21, 2012 join the ranks of the "real programmers" You are the real programmer, Debbie... regex is just my Sunday crossword. Quote Link to comment Share on other sites More sharing options...
doubledee Posted January 21, 2012 Author Share Posted January 21, 2012 join the ranks of the "real programmers" You are the real programmer, Debbie... regex is just my Sunday crossword. I wouldn't say that, but if I hang around with all of you smarties long enough, maybe someday I will become a PHP ninja as well?! Debbie Quote Link to comment Share on other sites More sharing options...
ragax Posted January 21, 2012 Share Posted January 21, 2012 I hang around with all of you smarties long enough I don't seem to have trouble hanging on to Smarties, it's more M&Ms that give me trouble. It's that crunchy peanut inside the chocolate... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.