Question

I am trying to find out whether my client-side Javascript regex

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

for email validation (I'm using it just to make sure that the email is formatted properly, not as a primary validation method) will work on the server side with PHP.

I am not sure whether I can use the same one even though both languages use Perl-based regex syntax. Thank you for your help.

Was it helpful?

Solution 2

This regex will work nearly identically in both JavaScript and PHP. There are some minuscule differences, for example \s matches the "next line" control character U+0085 in PHP, but not in JavaScript, but they are unlikely to matter in this context (it's unusual anyway to allow newlines and tabs in email addresses - why not use a simple space instead of the generic whitespace shorthand \s).

If you have to do these kinds of comparisons/conversions regularly, I heartily recommend you taking a look at RegexBuddy which can convert regexes between flavors with a single click.

OTHER TIPS

You should be able to use the same syntax. You should use preg_match(String $pattern, String $email[, array $matches]) with your pattern. It puts all occurrences into the array $matches, if given. It returns true if a match is found. For E-Mails in particular it's always a better idea to use the functions of others, because for example "$@us" is a valid email address

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top