Link to home
Create AccountLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Form Validation with Javascript

How do I create a javascript validation that checks each required field and then prints the error message in a unique div.  This is what I have so far but it does not work:

function validateForm()
	var errors = [];
  var od_position=document.forms["myForm"]["od_positio]"].value;
if (od_position==null || od_position=="")
    document.getElementById('id_of_div_here').innerHTML = 'error message here';

Open in new window

Avatar of COBOLdinosaur
Flag of Canada image

is this a typo, or the way you actually have it coded:

 var od_position=document.forms["myForm"]["od_positio]"].value;

I think should be:

 var od_position=document.forms["myForm"]["od_position"].value;

Open in new window

Though I generally prefer to do eveything with ids
If you are going to use the names,


is a lot cleaner.
Avatar of Robert Granlund


Your example did the same thing twice?
Then I guess we need to look at more of the page.  Can you post a link to the page?
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account

I used what you offered and it does not work.  If I leave the field blank, it just goes forward and does nothing?  What do you think I am doing wrong?