Library code snippets

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.

Comments

  1. 24 Sep 2003 at 09:58

    When I follow these instructions, I get an "Object expected" error. Any clue why?

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Field Content Validation.

Leave a comment

Sign in or Join us (it's free).

Super Tal Always working hard!!

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

  • Dec 8

    December Silicon Valley Ruby Meetup

    Moffett Field, United States

    In a World of Middleware, Who Needs Monolithic Applications? by Jon Crosby With Rack emerging as the standard for composing web applications and services, most recently with Rails adoption, an architectural shift is taking place. Learn how to create next generation web services by reusing existing Rack middleware and supplementing with your own components and micro-frameworks like Sinatra. Bio : Jon likes music, the Open Web, Ruby, Erlang, Haskell, Objective-C, JavaScript and coffee.

Want to stay in touch with what's going on? Follow us on twitter!