Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Form Validation and Initial Field Values… Reply To: Form Validation and Initial Field Values…

#89561
standuncan
Member

The I think the there is only a HTML5 maxlength attribute, but again if you use it in the php validation you can check against the strlen.

Here is a snippet of the email validation with comments for you:

//Check the form fields<br />
if(empty($email)){<br />
$errors = '<span class="error">Enter your email</span>';<br />
} else{<br />
<br />
// First, we check that there's one @ symbol,<br />
// and that the lengths are right.<br />
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {<br />
// Email invalid because wrong number of characters<br />
// in one section or wrong number of @ symbols.<br />
$errors = '<span class="error">Invalid email</span>';<br />
}<br />
<br />
// Split it into sections to make life easier<br />
$email_array = explode("@", $email);<br />
$local_array = explode(".", $email_array[0]);<br />
for ($i = 0; $i < sizeof($local_array); $i++) {<br />
<br />
if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&<br />
'*+/=?^_`{|}~.-]{0,63})|("[^(\|")]{0,62}"))$",<br />
$local_array[$i])) {<br />
<br />
$errors = '<span class="error">Invalid email</span>';<br />
}<br />
}<br />
<br />
// Check if domain is IP. If not,<br />
// it should be valid domain name<br />
if (!ereg("^[?[0-9.]+]?$", $email_array[1])) {<br />
<br />
$domain_array = explode(".", $email_array[1]);<br />
<br />
if (sizeof($domain_array) < 2) {<br />
$errors = '<span class="error">Invalid email</span>';<br />
}<br />
<br />
for ($i = 0; $i < sizeof($domain_array); $i++) {<br />
if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|<br />
([A-Za-z0-9]+))$",<br />
$domain_array[$i])) {<br />
$errors = '<span class="error">Invalid email</span>';<br />
}<br />
}<br />
}<br />
}<br />