Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/255485-limit-e-mail-to-60-characters/
Share on other sites

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)

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?!  ;D

 

 

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

 

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

 

 

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

 

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

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"...  8)

 

 

Debbie

 

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.

join the ranks of the "real programmers"

 

You are the real programmer, Debbie... regex is just my Sunday crossword.  ;D

 

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?!  8)

 

 

Debbie

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.