Page 1 of 1

8 characters - form field

Posted: Sun Mar 20, 2005 1:10 pm
by Addos
I’m trying to pop in a function that will stop anybody putting in more than 8 characters into a form field but I’m not too sure of the exact script I need.
I have been playing with this:

if ($_POST['email'] < 8 ) {
echo 'Your password can only be 8 characters or less';
?>
<input name="email" type="text" id="email" />

But it just opens in the browser with the error 'Your password can only be 8 characters or less' even before I get to run the form field.
Can anyone give me a few pointers as I’m still new to all this PHP stuff?
Thanks a mil
Brian

Posted: Sun Mar 20, 2005 1:29 pm
by John Cartwright

Code: Select all

if (!empty($_POST['email']) && (strlen($_POST['email']) > 8 )) { 
echo 'Your password can only be 8 characters or less';
read about empty() and strlen()

Posted: Sun Mar 20, 2005 2:27 pm
by nickvd
how about the maxlength attribute in the password field?

<input name="email" type="text" id="email" maxlength="8" />

Posted: Sun Mar 20, 2005 2:37 pm
by Addos
Thanks folks this worked a treat. I have actually got a max length too in the form field but if I’m right this will just cut of any characters after a certain length. Now I have a facility where a user can change a password and if I don’t limit the password input to a certain amount then (via php validation) somebody unknowns to themselves could enter a password longer that the max length set in the form and end up with an invalid password.
Thanks again
Brian :wink: