Forum Moderators: coopster

Message Too Old, No Replies

preg match not working for some reason

Perl Regular Expression email validation

         

ftaylor

1:49 pm on Jan 20, 2009 (gmt 0)

10+ Year Member



Hello folks,

I have been trying to work out what is wrong with my email validation preg.

Here is the regex, and below is my understanding of it:

^[A-Za-z0-9]+([._-]{1}[A-Za-z0-9]+){0,}@[A-Za-z0-9]+([._-]{1}[A-Za-z0-9]+){0,}\.[A-Za-z]{2, 6}$

starting with: one or more in the ranges A-Za-z0-9

followed by 0 or more of: 1 from .-_ followed by one or more in the ranges A-Za-z0-9

followed by @

then: one or more in the ranges A-Za-z0-9

followed by 0 or more of: 1 from .-_ followed by one or more in the ranges A-Za-z0-9

followed by .

followed by between 2 and 6 characters from A-Za-z

and it doesn't work. Is my logic flawed?

function validate_email($email)
{
return preg_match('/^[A-Za-z0-9]+([._-]{1}[A-Za-z0-9]+){0,}@[A-Za-z0-9]+([._-]{1}[A-Za-z0-9]+){0,}\.[A-Za-z]{2, 6}$/', $email);
}

ftaylor

3:39 pm on Jan 20, 2009 (gmt 0)

10+ Year Member



I discovered what the problem was. Rather simple really, you can't have a space between the {x, y} when you are limiting the number of elements.

coopster

11:13 pm on Jan 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This self-solution thing is becoming habit [webmasterworld.com] for you ... ;)

Welcome to WebmasterWorld!