Account Number validation [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
eymorais
Forum Newbie
Posts: 14
Joined: Fri Apr 11, 2008 8:38 am
Location: Moncton NB Canada

Account Number validation [Solved]

Post 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?
Last edited by eymorais on Mon Apr 21, 2008 11:53 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Account Number validation

Post 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}$/".
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Account Number validation

Post 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!
Last edited by markusn00b on Mon Apr 21, 2008 4:16 pm, edited 1 time in total.
User avatar
eymorais
Forum Newbie
Posts: 14
Joined: Fri Apr 11, 2008 8:38 am
Location: Moncton NB Canada

Re: Account Number validation

Post by eymorais »

Tried the first suggestion and it worked perfectly. Thanks!
Post Reply