Steve73NI
Steve73NI

Reputation: 17

Testing that variables are both not empty and also an appropriate length

Im creating a form for editing customer bank details, account number, sortcode etc If the customer leaves a necessary field blank the form is displayed with an error that they need to complete the appropriate fields

 if ($bankname == '' || $accname == ''||$sortcode==''||$accnum==''||$balance==''||$odlimit=='')
    // if they are empty, show an error message and display the form
         $error = 'ERROR: Please fill in all required fields!';
      editForm($bankname, $accname, $sortcode, $accnum, $balance, $passHint, $pin, $odlimit, $error, $id);       }

My problem is that I also want to check that if the user does input a value, for example in the account number field, that it needs to be 8 digits long exactly. So I was thinking

if($accnum!=''&&strlen($accnum)!=8){
  // Account numbers must be 8 digits in length
    $error = 'ERROR: Account number must be 8 digits long';
 editForm($bankname, $accname, $sortcode, $accnum, $balance, $passHint, $pin, $odlimit, $error, $id);
}

But my problem is if the user then leaves the account number blank no error is recorded at all and a null value is recorded in the database.

Is there a way that I can check for empty strings and then if they are not empty test to ensure they are a correct length?

Upvotes: 1

Views: 67

Answers (2)

Ruddy
Ruddy

Reputation: 9923

This what you mean? Is so, you just need to fill in the parts I named.

if(they are empty) {
  error 
}
else {
  if(not the right length) {
    error
  }
  else {
     run the edit
  }
}

Upvotes: 1

shashikant_
shashikant_

Reputation: 122

if($accnum!=''&&strlen($accnum)!=8 && $accnum== null){
// Account numbers must be 8 digits in length
$error = 'ERROR: Account number must be 8 digits long';
editForm($bankname, $accname, $sortcode, $accnum, $balance, $passHint, $pin, $odlimit,     $error, $id);
}

try this way, it will help

Upvotes: 0

Related Questions