Important alert: (current site time 7/15/2013 1:58:50 PM EDT)
 

winzip icon

Complete, general form validation

Email
Submitted on: 5/8/2000 12:46:19 PM
By: Eric H. Anderson 
Level: Intermediate
User Rating: By 6 Users
Compatibility: JavaScript
Views: 36324
 
     This script is a combination of a lot of common form validation elements - checking for non-entries, image swaping to indicate invalid entries, focusing on invalid fields, and feedback to the user through an alert box. I got tired of writing new code for every form (or even cutting and pasting a lot) so this is intended as a .js include file, and all the information necessary to validate the form is contained in the form itself. Most validation is of the did-they-fill-it-in variety. See the comments in the script for more details. Additional validation can be performed with a customized object-oriented JavaScript function. Explanations and an example are provided in the comments.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Complete, general form validation
'**************************************
<html>
<head>
 // the actual JS code is downloadable below.
 // it should be used as a .js include file
 // this HTML is simply to provide an example of the form structure required
 // <script language="javascript" src="errorCheck.js">
</head>
<body>
 <form name="Fred" action="YOUR.COM/PATH.HTM" method="post" onSubmit="return errorCheck(this, 'path.to.clear.dot.gif', 'path.to.red.star.gif', false);">
<input type="password" name="up_req_userpassword">
	 <input type="password" name="conf_userpassword">
	 <input type="hidden" name="mes_userpassword" value="Please make sure that you have entered a password, and typed exactly the same way again in the confirmation box.">
 </form>
</body>
</html>
		
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/8/2000 1:27:55 PMMatt

This is some solid code, and frankly it makes my life a lot easier since I'm no JS wizard.
(If this comment was disrespectful, please report it.)

 
10/10/2002 12:19:16 PMckyweriga

Code lookes great. I am just about to ad this to my form to validate everything. I do have one request however, I am not that great at JS so if you could please help me add a section where I could stop some people from using sertain email address from like themail.com etc.. I already have code for this but I would like to add this to your code. If you could do this for me, I will send you this code I got and then maybe you can implement this in to yours...

Hope to hear from you soon.
(If this comment was disrespectful, please report it.)

 
11/7/2002 7:50:18 PMEric H. Anderson

I'd recommend (off the top of my head) modifying the checkEmail function. Add a JavaScript Array that contains the bad domains. Then in the "else" section of the if clause, instead of just returning true, loop through the array, and compare the domain portion of the entered address to the current values in the array. You should be able to get the domain portion using the indexOf and substring methods.

If you find a bad domain, return false, otherwise, go ahead and return true after the loop on the bad domain array is complete.

you'd end up with something like

var aryBadDomain = Array('not.me','bad.com');

for (i=0;i if (aryBadDoman[i] == strFormEmailDomain) {
return false;
}
}

Or you could use a regular expression to do a much more complex filter on the domain.
(If this comment was disrespectful, please report it.)

 
12/16/2002 7:54:03 AMJosef

what exactly are the checkbox and checkselected functions checking.

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.