Simple JavaScript Form Check

/ Published in: JavaScript
Save to your folder(s)

This is a simple and effective way yo make sure people are actually filing out a form before submitting it. If you need to verify content it should be done server side (so that if you come across the odd person with javascript turned off it wont break something) but if you want to do a js check then you can easily add the code inside each elements if statement.

Also, if your form is larger than just a few items your probably better off doing it from a more advanced, but much smaller script.

Oh, and with it's current setup your submit button needs the type of button, or it needs to be changed to return a true/false once the checks have been processed.

Copy this code and paste it in your HTML
  1. function check(){
  2. var message = '';
  3. if(document.forms["form_name"].elements["first_input_name"].value==""){message=message+'The Proper name for the fist input.'+"\n"}
  4. if(document.forms["form_name"].elements["second_input_name"].value==""){message=message+'The Proper name for the second input.'+"\n"}
  5. if(document.forms["form_name"].elements["third_input_name"].value==""){message=message+'The Proper name for the third input.'+"\n"}
  6. /* ... */
  7. if(message==''){
  8. document.form_name.submit()
  9. }else{
  10. alert("You are missing the following information:\n\n"+message+"\nPlease Fill this information out and submit again.")
  11. }
  12. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.