Jump to content

Recommended Posts

Hey guys, sorry if this is too basic, but regex just confuses me.

 

I have a form validation class, with this function to check for valid emails

 

$pattern ="/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
	if(preg_match($pattern, $value))
	{
		return true;
	}

 

is the code used in the function.

 

I didn't make the expression myself, I copied it from elsewhere, can anyone tell me:

1) Is this accurate and complete, will this do the job without any problems (it seems to under testing)

2) Apart from being less lines of code, what advantage/disadvantage is there using this method over other methods I have seen to check for emails, such as explode("@", $string) and then analyzing each section of the string seperately.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/192610-email-validation/
Share on other sites

1) It is accurate and complete in the sense that it will do something specific.  Whether that something is precisely what you want is another matter entirely!  For example, email addresses are allow to use the plus character (+) for the mailbox part (before the @) but your regex does not allow it.  There is also no check at the end of the regex for "other stuff" so the string "a@b.c this is not part of an email address" will still return true!

 

2) Regex are good for consisely matching sometimes complex patterns that might be otherwise difficult to match. In the end, it is just another tool to get a job done and should be considered globally better than other approaches doing the same task.

 

P.S. Like I mentioned in point 1, there are a number of issues with your particular regex which really should not be done. Things like wrapping the character classes in parentheses are just not necessary.  I'm not sure if you would want a post picking apart the regex so will stop there excepting to say that validating emails with regex has been done a million times and most of the time someone else's "solution" might not be appropriate for your script. :shy:

Link to comment
https://forums.phpfreaks.com/topic/192610-email-validation/#findComment-1014741
Share on other sites

What can I say?

Thank you Salathe, that is a very concise answer which answers my question pretty much perfectly.

 

The main thing for me at the moment is getting all my scripts working, so as long as I know that the code is working with the script and $verify->isEmail($string) is working accordingly then I will leave it as it is for now, using the regex method rather than another approach.  Hopefully sometime soon I can take a look at becoming more familar with the regex patterns and be less confused by them, and not have to rely on other peoples work.  Although I may then be back on here saying "wtf - why doesn't this work"  :P

 

Many thanks again.

 

Link to comment
https://forums.phpfreaks.com/topic/192610-email-validation/#findComment-1015199
Share on other sites

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.