samporras Posted May 7, 2007 Posted May 7, 2007 Hey guys. I'm delving a little more into php and am creating a web form. While trying to create some simple validation, i came across the preg_match() function and am getting an error while implementing it. below are the 2 function i created: > function CheckName($name) { if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$name)) return TRUE; else return FALSE; } function CheckPhone($areaCode, $phonePrefix, $phoneSufix, $phone) { if(!preg_match("/[^0-9\-\]+$/",$phone)) return TRUE; elseif(strlen($areaCode) == 3) return TRUE; elseif(strlen($phonePrefix) == 3) return TRUE; elseif(strlen($phoneSuffix) == 4) return TRUE; else return FALSE; } i'm getting the following errors respectively when executing the code: Warning: preg_match() [function.preg-match]: Compilation failed: missing terminating ] for character class at offset 31 Warning: preg_match() [function.preg-match]: Compilation failed: missing terminating ] for character class at offset 11 any help would be greatly appreciated Sam Quote
TCH-Don Posted May 7, 2007 Posted May 7, 2007 Try > function CheckName($name) { if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$name)) { return TRUE; } else { return FALSE; } } function CheckPhone($areaCode, $phonePrefix, $phoneSufix, $phone) { if(!preg_match("/[^0-9\-\]+$/",$phone)) { return TRUE; } elseif(strlen($areaCode) == 3) { return TRUE; } elseif(strlen($phonePrefix) == 3) { return TRUE; } elseif(strlen($phoneSuffix) == 4) { return TRUE; } else { return FALSE; } } Quote
click Posted May 7, 2007 Posted May 7, 2007 (edited) The backslashes before the closing ']' brackets in your expressions are causing them to be treated as literal ']' characters, rather than closing brackets. So >if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$name)) ... should be ... >if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü]+$/s",$name)) Same with the second preg_match() Edited May 7, 2007 by click Quote
samporras Posted May 8, 2007 Author Posted May 8, 2007 one more question... I found the below function to validate an email address. >function CheckEmail($email_field) { if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9- ]+)*(\.[a-z]{2,3})", $email)) { return TRUE; } else return FALSE; } Whenever the code is hit, i get the following error: Warning: eregi() [function.eregi]: REG_ERANGE in /home/xxxx/xxxx/xxxx/contact.php on line 20 i tried escaping the quotation marks thinking it might be it, but i get a parse error when hitting that code. Quote
click Posted May 8, 2007 Posted May 8, 2007 In '[_a-z0-9-]', the last '-' is being seen as defining a range rather than a literal dash. Move it to the beginning to prevent this. So it would be: [-_a-z0-9] Quote
samporras Posted May 8, 2007 Author Posted May 8, 2007 In '[_a-z0-9-]', the last '-' is being seen as defining a range rather than a literal dash. Move it to the beginning to prevent this. So it would be: [-_a-z0-9] I attempted to do this and it didn't work. After looking at the expression a little bit more, i noticed I had an extra space after the '-' so it looked like this: [_a-z0-9- ] Was the extra space the reason it was interpreted as defining a range? The code seems to be functioning properly now after removing the space. Thanks again! I was stuck for hours!! Quote
click Posted May 8, 2007 Posted May 8, 2007 BTW, that expression is not an entirely correct way to validate addresses. It will, for instance, fail addresses with more than 3 letters in the domain, such as .info. You can find a pretty good example of a php email validator at http://www.ilovejackdaniels.com/php/email-...ess-validation/ Quote
click Posted May 8, 2007 Posted May 8, 2007 I attempted to do this and it didn't work. After looking at the expression a little bit more, i noticed I had an extra space after the '-' so it looked like this:Was the extra space the reason it was interpreted as defining a range? The code seems to be functioning properly now after removing the space. Thanks again! I was stuck for hours!! Could have been, I'm not really sure. I just knew it worked with it at the beginning. Quote
carbonize Posted May 8, 2007 Posted May 8, 2007 At present I am using this bit of code I found to check email addresses. > function check_emailaddress($email) { $addr_spec = '([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'. '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d'. '\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22)'. '(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e'. '\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|'. '\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00'. '-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28'. '\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d'. '\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff'. ']|\\x5c[\\x00-\\x7f])*\\x5d)(\\x2e([^\\x00-\\x20'. '\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40'. '\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-'. '\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d))*'; if (preg_match("!^$addr_spec$!i", $email)) { return true; } else { return false; } } thing is I swear I found a better bit of code and had put it in Lazarus but it's not there now. I'm always cocking up like that. Quote
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.