Reputation: 127
First I will show what I have, then I will explain:
if(isset($_POST['submitRegister'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$email = $_POST['email'];
if(!isset($username) || empty($username))
$error1 = 'Please enter your Username';
if(!isset($password) || empty($password))
$error2 = 'Please enter your Password';
if(!isset($password2) || empty($password2))
$error3 = 'Please confirm your Password';
if(!isset($email) || empty($email))
$error4 = 'Please enter your E-mail address';
if(!isset($_POST["terms"]))
$error5 = 'You must accept the Terms and Conditions';
This works fine. If I was to submit the form without having made any entries, the errors will be shown below their respective fields.
In all the tuts I have read, they say, if the field does not contain 'whatever' print an error, else congratulations your form has been submitted.
However, I would like to take this a few steps further.
For example: If the field has been set and isn't empty, then check to see if it contains the correct characters I.e:
if (!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
$error1 = 'Username can only contain: A-Z a-z 0-9 _ -;
This also works except, whatever I put next, always overrides the previous error. In other words, if I was to again submit the form without having entered anything in any of the fields, the username field will receive this second error, while the rest will receive the first error. After this I want to check to see if it's the right length I.e:
if(strlen($username) < 6)
$error1 = 'Username must have more than 6 characters';
Again, this will override the first error because having no characters is also less than 6.
I know I can do this all in 1 go, but that's not how I want it. I've tried putting it within if else statements and all sorts, but I just can't get it to work.
Hoping someone can help, many thanks.
WOW, this site is brilliant! I only went to the toilet and fed the cat! Thank you all for your answers, I will look into them and come back if I have any more problems. I just wanted to add that I have tried doing $error[] etc. but couldn't get it to work. Think it may have something to do with this:
$smarty->assign('error',$error);
$smarty->assign('message',$message);
$smarty->display('register.tpl');
I'm using Smarty as a template system. I'd rather not, but the tut I'm following uses it and I don't know how to display my .tpl without it, although I havn't looked into this yet.
Again, many thanks!
Upvotes: 2
Views: 115
Reputation: 408
You can concatenate Strings with the dot:
$error1='';
if(!isset($username) || empty($username))
$error1 = 'Please enter your Username';
if (!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
$error1 = $error1.'Username can only contain: A-Z a-z 0-9 _ - ';
EDIT:
From your comments I infer, that this might be what you want:
if (!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
$error1 = 'Username can only contain: A-Z a-z 0-9 _ -';
if(strlen($username) < 6)
$error1 = 'Username must have more than 6 characters';
if(!isset($username) || empty($username))
$error1 = 'Please enter your Username';
This displays only one error. If you leave the field empty, it will be "Please enter your username", if you enter something with less than 6 chars, it will be "Username must have more than 6 characters" and finally, if you enter more than 6 characters but the username contains weird stuff, it will be "Username can only contain: A-Z a-z 0-9 _ -"
I hope this helps.
Upvotes: 1
Reputation: 23500
I think you can create an array of errors for each set of variables checks, for example
if(!isset($username) || empty($username))
$error1[] = 'Please enter your Username';
if (!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
$error1[] = 'Username can only contain: A-Z a-z 0-9 _ - ';
if(strlen($username) < 6)
$error1[] = 'Username must have more than 6 characters';
Now in your $error1
array you have all errors found
Upvotes: 0
Reputation: 2694
You can store your errors in array like this:
$errorList = [
'username' => [],
'password' => [],
// ...
];
if (!preg_match('#^[a-zA-Z0-9_-]+$#', $username))
array_push($errorList['username'], 'Username can only contain: A-Z a-z 0-9 _ - ');
if(strlen($username) < 6)
array_push($errorList['username'], 'Username must have more than 6 characters');
On displaying:
if ($errorList['username']) {
foreach ($errorList['username'] as $error) {
echo $error . "<br/">
}
}
Upvotes: 0