Guest elthomo Posted March 17, 2008 Share Posted March 17, 2008 preg_match not working and I cant see why. This is code that doesn't seem to work. Have I made a mistake some where? ( It should check first character between 1 and 9 second,third and forth should be between 0 and 9 and the fifth and sixth letters A-Z) The other function seem to work correctly { if(!preg_match("/^[1-9]{1,1}[0-9]{3,3}\s?[A-Z]{2,2}$/ ",$postcode)) return TRUE; else return FALSE; } This is the whole code if it helps. <?php extract($_POST); /* Validation */ function check_field1($achternaam) { if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\ ]+$/s",$achternaam)) return TRUE; else return FALSE; } function check_field2($voorletters) { if(!preg_match("/[^a-zA-Z\.\-\Ä\ä\Ö\ö\Ü\ü\ ]+$/s",$voorletters)) return TRUE; else return FALSE; } function check_field3($email) { if(!preg_match("/[^0-9A-Za-z]+$@/ ",$email)) return TRUE; else return FALSE; } function check_field4($postcode) { if(!preg_match("/^[1-9]{1,1}[0-9]{3,3}\s?[A-Z]{2,2}$/ ",$postcode)) return TRUE; else return FALSE; } /* Validation */ $error=0; // check up variable /* get it checking */ if(!check_field1($achternaam)) { $aerror = "Illegal input $achternaam in 'achternaam'"; $error++; // $error=$error+1; } if(!check_field2($voorletters)) { $berror = "Illegal input $voorletters in 'voorletters'"; $error++; } if(!check_field3($email)) { $cerror = "Illegal input $email in 'email'"; $error++; } if(!check_field4($postcode)) { $derror = "Illegal input $postcode in 'postcode'"; $error++; } if($error == 0) { header("Location: what.php?achternaam=$achternaam&email=$email&dossiernummer=$dossiernummer"); }else{ $showerror = " <br><font color=#FF0000 size=1> Number of errors: $derror $cerror $berror $error.<br> $aerror</font> "; } ?> Link to comment https://forums.phpfreaks.com/topic/96508-preg_match-validation-not-working-and-i-cant-see-why/ Share on other sites More sharing options...
effigy Posted March 17, 2008 Share Posted March 17, 2008 It looks OK; although, some of the intervals are unnecessary: /^[1-9][0-9]{3}\s?[A-Z]{2}$/ What is the input data? Link to comment https://forums.phpfreaks.com/topic/96508-preg_match-validation-not-working-and-i-cant-see-why/#findComment-494201 Share on other sites More sharing options...
Guest elthomo Posted March 18, 2008 Share Posted March 18, 2008 Thanks for the reply :-) Actually I ended up doing it this way function valid_postcode ($str) { return (ereg ('^[1-9][0-9]{3}[ ]?[A-Za-z]{2}$', $str)); } if (!valid_postcode($postcode)){ $berror = "<font color=\"#FF0000\" size=2>- U heeft geen geldige postcode ingevuld.</font><br>"; } Link to comment https://forums.phpfreaks.com/topic/96508-preg_match-validation-not-working-and-i-cant-see-why/#findComment-494787 Share on other sites More sharing options...
uniflare Posted March 18, 2008 Share Posted March 18, 2008 a tip i recently discovered is to use \A instead of ^ to assert start of subject, as ^ in some webserver installations (except apache) specifically means "Negate Class" as i found out, i started using \A with no problems eg: "\A[1-9][0-9]{3}[ ]?[A-Za-z]{2}$" this works for PREG_match not sure for ereg and preg is perl compatible... Link to comment https://forums.phpfreaks.com/topic/96508-preg_match-validation-not-working-and-i-cant-see-why/#findComment-495138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.