Page 1 of 1

Account Number validation [Solved]

Posted: Mon Apr 21, 2008 11:12 am
by eymorais
Having a little problem with Account number validation.

The account number should be entered the following way on my form. 1234567-8

following a phone number validation example...

Code: Select all

 
else if(!preg_match("^[0-9]{3}-[0-9]{3}-[0-9]{4}$", $htelephone) || $htelephone != "") 
{ 
    $errmsg = 'Please enter your valid phone number';  
 
I came up with the following for my account number validation....

Code: Select all

 
if (!preg_match("^[0-9]{7}-[0-9]{1}$",$_POST["acct"]) || empty($_POST["acct"]))  
    {
      // redirect back to form
      header ("Location: ../form.php?error=account");
      exit();
    }   
 


But it redirects me to my form with an invalid error even if it's entered as 1234567-8.

any ideas?

Re: Account Number validation

Posted: Mon Apr 21, 2008 11:35 am
by onion2k
That isn't a valid regular expression because the opening delimiter doesn't match the closing delimiter (in PHP 4 at least). Try "/^[0-9]{7}-[0-9]{1}$/".

Re: Account Number validation

Posted: Mon Apr 21, 2008 11:36 am
by markusn00b
By changing it to:

Code: Select all

 
if (!preg_match('/[0-9]{7}-[0-9]{1}/', "019392-7"))  
 
It worked.

Didn't see onion got there first!

Re: Account Number validation

Posted: Mon Apr 21, 2008 11:53 am
by eymorais
Tried the first suggestion and it worked perfectly. Thanks!