Welcome to the Bod's World 2 JavaScript Form Tester.

Text input box :
Textarea input box :
Radio input control :
Checkbox input control :
Drop down listbox :
Button input control :
The following characters are valid for the Text and Textarea boxes ...
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-/*,.'@~#%$£€!µ°±(){}[ ]

And here is the code for how it is done ... (line numbers for reference only) ...
1   <script language="javascript">
2   <!--
3   function formchecker(theForm) {
4	var allvalid = true;
5	var alertstr = "";
6	var validstr = "All form data is correct.\n\n";
7	var num_of_elements = theForm.length;
8	var radio_selected = false;
9	var checkbox_selected = false;
10
11	for (var i=0; i<num_of_elements; i++) {
12		var theElement = theForm.elements[i];
13		var element_type = theElement.type;
14		var element_name = theElement.name;
15		var element_value = theElement.value;
16
17  // Check Text boxes ...
18		if (element_type == "text") {
19			var checkstr = 
			  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-
			  /*,.'@~#%$£€!µ°±(){}[ ]";
20			if (element_value.length == 0) {
21				alertstr += "Form element '" 
				  + element_name + "' contains no data.\n\n";
22				allvalid = false;
23			} else {
24				var badchars = "";
25				for (var j=0; j<element_value.length; j++) {
26					for (var k=0; k<checkstr.length; k++) {
27						if (element_value.charAt(j) == 
						  checkstr.charAt(k)) {
28							break;
29						}
30					}
31					if (k == checkstr.length) {
32						for (var l=0; l<badchars.length; l++) {
33							if (element_value.charAt(j) == 
							  badchars.charAt(l)) {
34								break;
35							}
36						}
37
38						if (l == badchars.length) {
39							badchars += element_value.charAt(j);
40						}
41						allvalid = false;
42					}
43				}
44				if (!allvalid) {
45					alertstr += "Form element '" + element_name + 
					  "' contains the following illegal characters ...\n\t" 
					  + badchars + "\n\n";
46				}
47			}
48			if (allvalid) {
49				validstr += "Into form element '" + element_name + 
				  "' you entered the text ...\n\"" + element_value + "\".\n\n";
50			}
51		}
52
53  // Check Textarea boxes ...
54		if (element_type == "textarea") {
55			var checkstr = 
			  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-
			  /*,.'@~#%$£€!µ°±(){}[ ]\n\r\f";
56			if (element_value.length == 0) {
57				alertstr += "Form element '" + element_name + 
				  "' contains no data.\n\n";
58				allvalid = false;
59			} else {
60				var badchars = "";
61				for (var j=0; j<element_value.length; j++) {
62					for (var k=0; k<checkstr.length; k++) {
63						if (element_value.charAt(j) == 
						  checkstr.charAt(k)) {
64							break;
65						}
66					}
67					if (k == checkstr.length) {
68						for (var l=0; l<badchars.length; l++) {
69							if (element_value.charAt(j) == 
							  badchars.charAt(l)) {
70								break;
71							}
72						}
73
74						if (l == badchars.length) {
75							badchars += element_value.charAt(j);
76						}
77						allvalid = false;
78					}
79				}
80				if (!allvalid) {
81					alertstr += "Form element '" + element_name + 
					  "' contains the following illegal characters ...\n\t" 
					  + badchars + "\n\n";
82				}
83			}
84			if (allvalid) {
85				validstr += "Into form element '" + element_name + 
				  "' you entered the text ...\n\"" + element_value + "\".\n\n";
86			}
87		}
88
89  // Check Drop-down lists ...
90		if (element_type.indexOf("select") > -1) {
91			var index = theElement.selectedIndex;
92			if (index <= 0) {
93				alertstr += "The first option in form element '" + element_name + 
				  "' is not valid.\n\n";
94				allvalid = false;
95			}
96			if (allvalid) {
97				validstr += "From form element '" + element_name + 
				  "' you selected option \"" + theElement.options[index].value + 
				  "\".\n\n";
98			}
99		}
100
101  // Check Radio buttons ...
102		if (element_type == "radio" ) {
103			if (theElement.checked == true) {
104				radio_selected = true;
105				validstr += "From form element '" + element_name + 
				  "' you selected the \"" + element_value + "\" button.\n\n";
106			}
107		}
108
109 // Check Checkboxes ...
110		if (element_type == "checkbox") {
111			if (theElement.checked == true) {
112				checkbox_selected = true;
113				validstr += "From form element '" + element_name + 
				  "' you selected the \"" + element_value + "\" checkbox.\n\n";
114			}
115		}
116
117 // Check Buttons ...
118		if (element_type == "button") {
119			// Don't check buttons - use the onClick event to invoke functions.
120		}
121
122 	// .... End of loop through form elements ....
123	}
124
125	if (radio_selected == false) {
126		alertstr += "There is no Radio Button selected.\n\n";
127		allvalid = false;
128	}
129
130	if (checkbox_selected == false) {
131		alertstr += "There are no Checkboxes selected.\n\n";
132		allvalid = false;
133	}
134
135 // All elements checked - now determine if form is OK ...
136	if (allvalid) {
137		alert (validstr);
138		alert ("Who's a clever-clogs then?\n\n        %%%\n     / o     o \\\n    @   \\
		  /   @\n      \\  ~~  /\n         ###");
139		return false;
140	} else {
141		alert (alertstr);
142		return false;
143	}
144 }
145 //-->
146 </script>

The basic way this script works is to loop through all the form elements and check for the type of each element - the method of checking depends on this.

  • If it is a Text box or a Textarea box then firstly we check whether it contains any data (lines 20 and 56), then we check whether it contains any character which is NOT on the list of valid characters - if either check is false then error messages are added to the errors list.
  • If it is a drop-down list then we simply check whether the item selected is the first item in the list - if yes then an error message is added to the errors list.
  • If it is one of a series of radio buttons then we check whether any one is selected - if no then an error message is added to the errors list. (Set the value in line 8 to true if un-selected radio buttons are allowed).
  • If it is one of a series of checkboxes then we check whether any one is checked - if no then an error message is added to the errors list. (Set the value in line 9 to true if un-checked checkboxes are allowed).

If there are any errors in the errors list then the list is displayed and the result of the form check is false. This means that the form is not submitted. If all is OK then, in this case, we display an OK message - under normal conditions lines 137 and 138 would be omitted, and the value in line 139 would be true hence the form would be submitted.