Field Content Validation

Form Field Validation

Field Content Validation


Ok, I've had several people email me wanting to know how they can validate a field based on a certain criteria.  Well, I through this together and hope it will work for you.  There are many ways to do this such as Regular Expression, and other functions, but you should be able to implement this rather easily.

function validate_char(field) {
 var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
 var ok = "yes";
 var temp;
 for (var i=0; i<field.value.length; i++) {
   temp = "" + field.value.substring(i, i+1);
   if (valid.indexOf(temp) == "-1") ok = "no";
 }
 if (ok == "no") {
   alert("Invalid entry!  Only characters Aa-Zz are accepted!");
   field.focus();
   field.select();
 }
}


Now just simply call this like:

<INPUT type="text" name="txt_name" onBlur="validate_char(this)">

Feel free to change the valid variable to anything you'd like to restrict user input.

You might also like...

Comments

Super Tal Always working hard!!

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger