PDA

View Full Version : Errors into an array.


thesavior
08-16-2006, 04:13 AM
I want to do some checks like this, and then display them.


if((strlen($username) < 6) || (strlen($username) > 29)) error[] = 'Your username must be between 6 and 30 characters.';
if ($password1 != $password2) error[] .= 'Your passwords do not match.';
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) error[] .= 'Your e-mail was not valid.';
if (preg_match('/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/', $username)) error[] .= 'Usernames may not be in the form of an IP address.';
if (preg_match('#\[b\]|\[/b\]|\[u\]|\[/u\]|\[i\]|\[/i\]|\[color|\[/color\]|\[quote\]|\[/quote\]|\[code\]|\[/code\]|\[img\]|\[/img\]|\[url|\[/url\]|\[email|\[/email\]#i', $username)) error[] .= 'Usernames may not contain any BBCode.';
if(strlen($email) > 29) error[] .= 'Your username must be less than 30 characters.';



Then check if error[] is empty, if it isn't, show something like this (of course, only if they nothing was matched):

Some errors occured:
Your username must be between 6 and 30 characters.
Your passwords do not match.
Your e-mail was not valid.
Usernames may not be in the form of an IP address.
Usernames may not contain any BBCode.
Your username must be less than 30 characters.

How do I go about doing this?

Im sure it uses a foreach to loop through the array, but am i even populating the array right? And how do i make it print out the correct error?

marek_mar
08-16-2006, 04:28 AM
It would be nice if you'd indent the code...
You aren't populatin an array at all. You variable is missing the $.
You are usiong eregi() next to the preg_* functions...
The logic doesn't match the message in
if((strlen($username) < 6) || (strlen($username) > 29)) error[] = 'Your username must be between 6 and 30 characters.';
The IP regex matches invalid IP's

but after you get an array of erros together you can just do it like this:

<?php
if(count($error))
{
print 'Errors';
print implode("<br />\n", $error);
}
?>