If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!

At your site do you have a registration form? Want to check for password length before the form is submitted? You can do this easily with JavaScript and a little alert box.

First of all here’s a little sample form you might have:

 

<form name=“form_name” method=“post”>
Username:<br /><input type=“text” name=“username” /><br />
Password:<br /><input type=“password” name=“password” /><br />
<input type=“submit” name=“submit” value=“Submit” />
</form>

That’s just a simple form with a username, password, and submit field. Now what we’re going to do is add one more attribute to our password field to make it look something like this:

 

<input type=“password” name=“password” onblur=“javascript:checkPassword()” />

The onblur attribute activates it’s value when you unselect the object in question. So, when we unselect the password field, the javascript function “checkPassword()” will be called. Now we have the HTML, however we have to make checkPassword() actually do something, so we have to define a javascript function in the head of our document.

Anywhere between your and tag, you would add something like this:

 

<script language=“JavaScript” type=“text/javascript”>
<!–//
function checkPassword(){password = document.form_name.password.value; //Gets the value of the form element “password” in the form named “form_name”
if(password.length < 6){ //If the password length is less than 6
alert(“Please enter a password of 6 characters or more”); //Alert that it needs to be 6 or more
}

}
//–>
</script>

You can easily change the number 6 to whatever number you want, and it will always alert when you blur away from the element :) If you have a different name for either your form or your field you have to change the JavaScript accordingly, although I’m pretty sure you could probably figure out how to do that?

And that’s it, pretty simple :) You could also do some more complex things with it such as check for certain characters using Regex.