8 characters - form field

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

8 characters - form field

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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()
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

how about the maxlength attribute in the password field?

<input name="email" type="text" id="email" maxlength="8" />
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post 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:
Post Reply