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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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