contact form php and jquery fight

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
greentrancer
Forum Newbie
Posts: 3
Joined: Wed Feb 22, 2012 9:10 pm

contact form php and jquery fight

Post by greentrancer »

hello. in my contact form i have 2 validators, one in jquery and one in php. the problem is next. this is the jquery where checks the email and it works perfectly ( i know that this regex is from php ).

Code: Select all

 function validateEmail() {
	var filter = /^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/;
	if(filter.test(email.val()))
when i'm trying to put it in php, it doesn't work anymore.

Code: Select all

function validateEmail($email){
		return ereg("^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$", $email);
	}
if i put the next code instead of the one from up, it works.

Code: Select all

function validateEmail($email){
		return ereg("^[a-zA-Z0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$", $email);
	}
does anyone have any idea ? thank you.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: contact form php and jquery fight

Post by ragax »

Hi greentrancer!

A few comments:

1. According to RegexBuddy, that long regex would match -@1.q
Don't know what you're doing, but if you're trying to validate an email address, a long expression like that might be a waste of time if it will validate -@1.q

2. The ereg functions are deprecated. Most people here would suggest you replace any ereg function with a preg_match.

3. There are a million ways to validate an email address. In this case, rather than troubleshoot an expression that may not be optimal (I'm talking about the second one now), I'd suggest you take the easy road and use one of several expressions in the RB library, replacing your return line with:

Code: Select all

return preg_match('~^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$~i',$email);
Let me know if that kind of idea works for you!

Wishing you a fun day.

:)
greentrancer
Forum Newbie
Posts: 3
Joined: Wed Feb 22, 2012 9:10 pm

Re: contact form php and jquery fight

Post by greentrancer »

thank you playful for your answer. i tried preg_match instead of ereg and it's not working. i am beginner in php, but i will learn it in the future. for now, i just want to make this working for my contact form. i took that regex from http://fightingforalostcause.net/misc/2 ... -regex.php - it says that filter_var() is using it. but i cannot use filter_var because these validators are in another file.

i just want to use of the best regex to validate the email. for example, i think that your regex doesn't validate valid emails addresses like these:
"john smith"@example.com
john.smith(comment)@example.com
john#smith@example.com

thank you again. have a nice day.
User avatar
ragax
Forum Commoner
Posts: 85
Joined: Thu Dec 15, 2011 1:40 pm
Location: Nelson, NZ

Re: contact form php and jquery fight

Post by ragax »

Hi greentrancer,
i tried preg_match instead of ereg and it's not working.
That's odd. If you run this:
Code:

Code: Select all

<?php
$email='joe@mamA.com';
$email2='joe@@mamA.com';
echo preg_match('~^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$~i',$email).'<br />';
echo preg_match('~^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$~i',$email2).'<br />';
?>
You get this:
Output:
1
0
i think that your regex doesn't validate valid emails addresses like these:
"john smith"@example.com
john.smith(comment)@example.com
john#smith@example.com
You are right. I was not aware that those were valid emails. I'll research that a bit if I have time this week.

wishing you a fun day.
greentrancer
Forum Newbie
Posts: 3
Joined: Wed Feb 22, 2012 9:10 pm

Re: contact form php and jquery fight

Post by greentrancer »

i will try your method until i find a solution to mine ( or another solution ). some guy on another php forum told me about those emails because i didn't know too. thank you!
Post Reply