Jump to content

preg_match validation not working and I cant see why...


Guest elthomo

Recommended Posts

Guest elthomo

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> ";
}

?>

Guest elthomo

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>";
        }

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.