This is something I whipped up to make sure people weren’t submitting forms missing vital information, but I didn’t care if the information was wrong. It’s for a back end thing, the general public should be thoroughly checked (it’s a lowest common denominator thing) but if this is an important work related thing your not very likely to mess it up to terribly (but, check for anything that will break your script just in case they do). In this case it was a form that stores client information, if the names in the wrong place it can be edited and it’s not a big deal, but if you really want to ensure accuracy you can run additional checks inside each of the if
s for that articular thing.
1 2 3 4 5 6 7 8 9 10 11 12 |
function check(){ var message = ''; if(document.forms["form_name"].elements["first_input_name"].value==""){message=message+'The Proper name for the fist input.'+"\n"} if(document.forms["form_name"].elements["second_input_name"].value==""){message=message+'The Proper name for the second input.'+"\n"} if(document.forms["form_name"].elements["third_input_name"].value==""){message=message+'The Proper name for the third input.'+"\n"} /* ... */ if(message==''){ document.form_name.submit() }else{ alert("You are missing the following information:\n\n"+message+"\nPlease Fill this information out and submit again.") } } |
Snipplr: http://snipplr.com/view/46271/simple-javascript-form-check/
Two things I’d like to note about this. First, if you have a very large form it should probably be done with a script that looks at all elements instead of using a line per element. Second, you have to make the submit button type="button"
instead of type="submit"
or you have to make it return true
/false
upon completion. I don’t see much of an advantage either way so I used a button for simpler testing and never changed it back.