Hi guys,
I don't know much about JavaScript but having a go at validating form input. I can check whether the user has entered anything but struggling with string length.
The code:
[text]
<form action="register.php" onsubmit="return validate_registration(this);" method="post">
<b>Password:</b><br />
<input type="password" size="20" name="password"> <br /><br />
<input type="submit" name="submit" value="Continue">
</form>
[/text]
[text]
function validate_registration (field)
{
if (field.password.value.length > 40)
{
alert ("Your password must be 40 characters or less.");
field.password.focus();
return false;
}
else
{
return true;
}
}
[/text]
Any idea why it isn't working?
String Length
Moderator: General Moderators
Re: String Length
Hm, would something like this work:
Code: Select all
<input type="text" onkeyup="if(this.value.length > 5){alert('over 5!');}"></input>Re: String Length
I'm not familiar with your object model (like, "field.password.value.length"). I use the function getElementById():
Code: Select all
...
<input type='text' name='password' id='password' />
...
fld=document.getElementById('password');
...
whatever ... fld.lengthRe: String Length
Thanks both. I think I'll use what omniuni suggested as it works just as I want. 