greentrancer Posted February 23, 2012 Share Posted February 23, 2012 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 ). 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. 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/ Share on other sites More sharing options...
abareplace Posted February 23, 2012 Share Posted February 23, 2012 Use preg_match instead of ereg. POSIX regular expressions (the ereg function) are depreciated; you should use PCRE. Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320298 Share on other sites More sharing options...
AyKay47 Posted February 23, 2012 Share Posted February 23, 2012 The first regex will match unicode characters, the second will match normal ASCII characters (a,b,c etc..). Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320374 Share on other sites More sharing options...
greentrancer Posted February 23, 2012 Author Share Posted February 23, 2012 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. do you have any idea how to fix it ? i cannot use filter_var because this validators are in another file. i think that i need a good regex which checks the email. thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320542 Share on other sites More sharing options...
AyKay47 Posted February 23, 2012 Share Posted February 23, 2012 an email regex: $pattern = "~\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b~i"; $string = "example@example.com"; if(preg_match($pattern,$string,$ms)) { //email is valid print_r($ms); } Edit: Good catch by Zane as well that you should look into. Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320586 Share on other sites More sharing options...
Zane Posted February 24, 2012 Share Posted February 24, 2012 Unlike ereg POSIX regex functions, the preg PCRE regex functions require the delimiters at the beginning and end, just like you have in your jQuery regex. So your preg_match, should look like this return preg_match("/^(?!(??:\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); Maybe you tried that already, but I'm just sayin'.. you need those forward slashes for PCRE regex. Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320587 Share on other sites More sharing options...
greentrancer Posted February 24, 2012 Author Share Posted February 24, 2012 i tried now again and something it's working. if i put the email johnsmith@example.com it's working, but when i put john.smith@example.com it's not working anymore. i don't understand. i just discovered that even if i use return preg_match("/^[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); the problem it's the same. :wtf: thank you! Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320653 Share on other sites More sharing options...
Zane Posted February 24, 2012 Share Posted February 24, 2012 The easiest solution to this is to Google "valid email regex pcre" and see what comes up. Find and try every decent regex until one works the way you want. Somewhere out there in the intertubes lies the epitome of an email regex; one much better than you could probably ask for to be written. Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320702 Share on other sites More sharing options...
AyKay47 Posted February 24, 2012 Share Posted February 24, 2012 i tried now again and something it's working. if i put the email johnsmith@example.com it's working, but when i put john.smith@example.com it's not working anymore. i don't understand. i just discovered that even if i use return preg_match("/^[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); the problem it's the same. :wtf: thank you! curious, have you tried the pattern that I gave you? Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320789 Share on other sites More sharing options...
greentrancer Posted February 24, 2012 Author Share Posted February 24, 2012 AyKay47, i tried now your regex and it's working. i also tried to replace your regex with this one /^(?!(??:\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}))\]))$/ and it's not working. i don't understand how it's working in jqery variable and in php not. to be clear, this it's working function validateEmail($email){ $pattern = "~\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b~i"; return preg_match($pattern,$email) } and this one not function validateEmail($email){ $pattern = "/^(?!(??:\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}))\]))$/"; return preg_match($pattern,$email) } Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320865 Share on other sites More sharing options...
AyKay47 Posted February 24, 2012 Share Posted February 24, 2012 Every language has a different flavor of regex. Things that are valid in one might be invalid in another. For your reading: http://www.regular-expressions.info/refflavors.html Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320873 Share on other sites More sharing options...
greentrancer Posted February 24, 2012 Author Share Posted February 24, 2012 i believe you, but this regex it's the one which filter_var() is using and it's for php. have a look here: http://fightingforalostcause.net/misc/2006/compare-email-regex.php thank you. Quote Link to comment https://forums.phpfreaks.com/topic/257590-contact-form-php-and-jquery-fight/#findComment-1320905 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.