JavaScript Code

A very fast way to validate a form with JavaScript

There are many universal form validation functions out there. But I created this very quick function to very easily check for input, without modifying your form fields in any way, and without have to check each field individually. The script just checks for a value, it doesn't validate the format (like an email address), because that's very easy to add, so you can do it yourself!

All you do is put the name of each field in the array, and it quickly loops through the array, checks the field, and fails if one of the fields is empty


function checkForm() {
	f = document.form1;
	var isGood = true;
	
	var required = new Array();
	 required[0] = "email";
	 required[1] = "password";
	 required[2] = "first_name";
	 required[2] = "last_name";
	 required[3] = "country";
	
	for(i in required) {
		if(f.elements[i].value=="") {
			isGood = false;
			break;
		}
	}
	
	if(!isGood)
		alert("Please fill in all of the required fields.");
	
	return isGood;
}
//-->
</script>

Then put this in your form code:


<form name="form1" method="post" action="" onSubmit="return checkForm();">
Share

11 Comments

  1. how do i work this thing???

  2. i also forgot to ask you if this would work on a game sight

  3. It would work on any site. As explained above, just include the code and reference it in your form. It's that simple!

  4. i'm sorry but can you explain how to set up a form for a game that already exists, like runescape, thx

  5. If you don't know how to create an HTML form, you may need to search online for a basic HTML tutorial, sorry

  6. I see the script ending tag, i was wondering where does the script start.
    where is your beginning tag

  7. i'm sorry but can you explain how to set up a form for a game that already exists

  8. Hi dude, this is the only thing I found on the net that works for Fireworks. Thanks.

    I have a few questions though.

    How would I add to your code above the following:

    Field was Number: and you can only enter seven numbers, with no characters (a-z).

    And the last thing is the email, I'm not sure how I would fit it into the arry.

  9. Firefox, not Fireworks. >.< Sorry!

  10. I can only get it to validate the first two fields. Any idea why?

  11. I did delete the last field "required[3] = "country";" because I didn't have that many fields.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image