Library code snippets
Field Content Validation
By Super Tal, published on 01 Apr 2002
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.
Related articles
Related discussion
-
.NET Developer in Ghana Required....
by sysview (0 replies)
-
Problem when using TemplateField and ImageButton
by ashiquemca (15 replies)
-
problem with special characters
by avlisodraude (1 replies)
-
PrintDocument printing using DOS print
by squrrel (1 replies)
-
$200 Website Design and Development
by manypeopledesign (1 replies)
Related podcasts
-
Raleigh Code Camp reflections, Chrome wins speed tests, GroovyMag
I attended the Raleigh Code Camp last week and had a blast meeting some awesome people (but of course there's never enough time to meet everyone you'd like to!). Some great sessions I attended covered the MS MVC platform and an introduction to MS DLR, both of which I discuss here in a bit more d...
Events coming up
-
Nov
20
Full Frontal JavaScript Conference
Brighton, United Kingdom
A one day JavaScript conference held in Brighton, UK whose essence is to discuss JavaScript "with nothing concealed or held back".The conference is being held at one of the world's first cinemas, which first opened in 1910.Speakers include...
When I follow these instructions, I get an "Object expected" error. Any clue why?
This thread is for discussions of Field Content Validation.