/ Published in: JavaScript
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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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.") } }
URL: http://fatfolderdesign.com/152/unimportant/simple-javascript-form-check