Tiffany B. Brown

A web log about web development and internet culture with frequent detours into other stuff.
Have you read any good books lately?
Links for 2007-04-11

A generic(-ish) JavaScript form validator function

I tried my hand at developing a reusable form validator function with JavaScript and the DOM. It checks:

If any errors are found, it will update a showErrors document fragment, and the form will not be submitted.

Yeah, it’s only mildly impressive, but if you knew where I was with JavaScript and DOM scripting six months ago, you’d be proud of me :-).

Keep in mind that this does not work if JavaScript is turned off. In other words, don’t rely on it for final data validation. Use a server-side solution.

See the form check script in action, or download it [ZIP] file. Use it or modify it to your heart’s content. Post fixes or suggested improvements in the comments.

Share this entry:
  • Digg
  • Technorati
  • del.icio.us
  • Ma.gnolia
  • Mixx
  • NewsVine
  • Reddit
  • StumbleUpon
  • TailRank
  • Furl
  • Slashdot
  • Global Grind
  • YahooMyWeb
  • Facebook
  • Google
  • Live

2 comments

  1. mike said on 11 Apr 2007 at 2:02 am

    if(errors){ document.getElementById('showErrors').innerHTML = '<ul>'+errors+'</li>'; return false; }

    Should be '<ul>'+errors+'</ul>‘;

    Otherwise looks pretty good.

  2. D’oh. Thanks for pointing that out. It’s fixed now.

previous post: Have you read any good books lately?
next post: Links for 2007-04-11