Return to Snippet

Revision: 38290
at December 30, 2010 08:31 by FatFolderDesigner


Updated Code
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.")
	}
}

Revision: 38289
at December 30, 2010 08:25 by FatFolderDesigner


Initial Code
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.topform.submit()
	}else{
		alert("You are missing the following information:\n\n"+message+"\nPlease Fill this information out and submit again.")
	}
}

Initial URL
http://fatfolderdesign.com/152/unimportant/simple-javascript-form-check

Initial Description
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.

Initial Title
Simple JavaScript Form Check

Initial Tags
javascript, data, forms

Initial Language
JavaScript