Validate_JS
This JavaScript script contains the validation functions for the Submit button when JavaScript is active in the browser. Also has the ConfirmReset function for JavaScript based Reset button.
1| function ValidateSubmit(form)
2| {
3| var ValidateSwitch = true;
4|
5| if(!TestNotEmpty(form.firstName))
6| {
7| document.getElementById("FName").style.visibility = "visible";
8| document.getElementById("FName").style.display = "inline";
9| ValidateSwitch = false;
10| }
11| else
12| {
13| document.getElementById("FName").style.visibility = "hidden";
14| document.getElementById("FName").style.display = "none";
15| }
16|
17| if(!TestNotEmpty(form.lastName))
18| {
19| document.getElementById("LName").style.visibility = "visible";
20| document.getElementById("LName").style.display = "inline";
21| ValidateSwitch = false;
22| }
23| else
24| {
25| document.getElementById("LName").style.visibility = "hidden";
26| document.getElementById("LName").style.display = "none";
27| }
28|
29| if(!TestNotEmpty(form.email))
30| {
31| document.getElementById("EmailBlank").style.visibility = "visible";
32| document.getElementById("EmailBlank").style.display = "inline";
33| document.getElementById("EmailFormat").style.visibility = "hidden";
34| document.getElementById("EmailFormat").style.display = "none";
35| ValidateSwitch = false;
36| }
37| else if(!TestEmail(form.email))
38| {
39| document.getElementById("EmailBlank").style.visibility = "hidden";
40| document.getElementById("EmailBlank").style.display = "none";
41| document.getElementById("EmailFormat").style.visibility = "visible";
42| document.getElementById("EmailFormat").style.display = "inline";
43| ValidateSwitch = false;
44|
45| }
46| else
47| {
48| document.getElementById("EmailBlank").style.visibility = "hidden";
49| document.getElementById("EmailBlank").style.display = "none";
50| document.getElementById("EmailFormat").style.visibility = "hidden";
51| document.getElementById("EmailFormat").style.display = "none";
52| }
53|
54| if(!TestSelect(form.subject))
55| {
56| document.getElementById("SubjectDefault").style.visibility = "visible";
57| document.getElementById("SubjectDefault").style.display = "inline";
58| ValidateSwitch = false;
59| }
60| else
61| {
62| document.getElementById("SubjectDefault").style.visibility = "hidden";
63| document.getElementById("SubjectDefault").style.display = "none";
64| }
65|
66| if(!TestNotEmpty(form.message))
67| {
68| document.getElementById("MsgBlank").style.visibility = "visible";
69| document.getElementById("MsgBlank").style.display = "inline";
70| ValidateSwitch = false;
71| }
72| else
73| {
74| document.getElementById("MsgBlank").style.visibility = "hidden";
75| document.getElementById("MsgBlank").style.display = "none";
76| }
77|
78| if(!TestNotEmpty(form.capAnswer))
79| {
80| document.getElementById("CaptchaBlank").style.visibility = "visible";
81| document.getElementById("CaptchaBlank").style.display = "inline";
82| document.getElementById("CaptchaNum").style.visibility = "hidden";
83| document.getElementById("CaptchaNum").style.display = "none";
84| document.getElementById("CaptchaWrong").style.visibility = "hidden";
85| document.getElementById("CaptchaWrong").style.display = "none";
86| ValidateSwitch = false;
87| }
88| else if(!TestNum(form.capAnswer))
89| {
90| document.getElementById("CaptchaBlank").style.visibility = "hidden";
91| document.getElementById("CaptchaBlank").style.display = "none";
92| document.getElementById("CaptchaNum").style.visibility = "visible";
93| document.getElementById("CaptchaNum").style.display = "inline";
94| document.getElementById("CaptchaWrong").style.visibility = "hidden";
95| document.getElementById("CaptchaWrong").style.display = "none";
96| ValidateSwitch = false;
97|
98| }
99| else if(!TestCapCorrect(form.capAnswer,form.capComp))
100| {
101| document.getElementById("CaptchaBlank").style.visibility = "hidden";
102| document.getElementById("CaptchaBlank").style.display = "none";
103| document.getElementById("CaptchaNum").style.visibility = "hidden";
104| document.getElementById("CaptchaNum").style.display = "none";
105| document.getElementById("CaptchaWrong").style.visibility = "visible";
106| document.getElementById("CaptchaWrong").style.display = "inline";
107| ValidateSwitch = false;
108|
109| }
110| else
111| {
112| document.getElementById("CaptchaBlank").style.visibility = "hidden";
113| document.getElementById("CaptchaBlank").style.display = "none";
114| document.getElementById("CaptchaNum").style.visibility = "hidden";
115| document.getElementById("CaptchaNum").style.display = "none";
116| document.getElementById("CaptchaWrong").style.visibility = "hidden";
117| document.getElementById("CaptchaWrong").style.display = "none";
118| }
119|
120| if(ValidateSwitch == true)
121| {
122| document.getElementById("firstName").style.backgroundColor = "silver";
123| document.getElementById("firstName").style.color = "grey";
124| document.getElementById("lastName").style.backgroundColor = "silver";
125| document.getElementById("lastName").style.color = "grey";
126| document.getElementById("email").style.backgroundColor = "silver";
127| document.getElementById("email").style.color = "grey";
128| document.getElementById("subject").style.backgroundColor = "silver";
129| document.getElementById("subject").style.color = "grey";
130| document.getElementById("message").style.backgroundColor = "silver";
131| document.getElementById("message").style.color = "grey";
132| document.getElementById("capAnswer").style.backgroundColor = "silver";
133| document.getElementById("capAnswer").style.color = "grey";
134| document.getElementById("submit").style.backgroundColor = "silver";
135| document.getElementById("submit").style.color = "grey";
136| document.getElementById("submit").style.borderWidth = "0px";
137| document.getElementById("reset").style.backgroundColor = "silver";
138| document.getElementById("reset").style.color = "grey";
139| }
140|
141| if(ValidateSwitch == true)
142| {
143| document.contact_form.firstName.disabled = true;
144| document.contact_form.lastName.disabled = true;
145| document.contact_form.email.disabled = true;
146| document.contact_form.subject.disabled = true;
147| document.contact_form.message.disabled = true;
148| document.contact_form.capAnswer.disabled = true;
149| document.contact_form.submit.disabled = true;
150| document.contact_form.reset.disabled = true;
151|
152| return true;
153| }
154| else
155| {
156| return false;
157| }
158|
159| return false;
160| }
161|
162| function ConfirmReset()
163| {
164| if(window.confirm("Are you sure you want to reset form?") == true)
165| {
166| document.getElementById("FName").style.visibility = "hidden";
167| document.getElementById("FName").style.display = "none";
168| document.getElementById("LName").style.visibility = "hidden";
169| document.getElementById("LName").style.display = "none";
170| document.getElementById("EmailBlank").style.visibility = "hidden";
171| document.getElementById("EmailBlank").style.display = "none";
172| document.getElementById("EmailFormat").style.visibility = "hidden";
173| document.getElementById("EmailFormat").style.display = "none";
174| document.getElementById("SubjectDefault").style.visibility = "hidden";
175| document.getElementById("SubjectDefault").style.display = "none";
176| document.getElementById("MsgBlank").style.visibility = "hidden";
177| document.getElementById("MsgBlank").style.display = "none";
178| document.getElementById("CaptchaBlank").style.visibility = "hidden";
179| document.getElementById("CaptchaBlank").style.display = "none";
180| document.getElementById("CaptchaNum").style.visibility = "hidden";
181| document.getElementById("CaptchaNum").style.display = "none";
182| document.getElementById("CaptchaWrong").style.visibility = "hidden";
183| document.getElementById("CaptchaWrong").style.display = "none";
184| return true;
185| }
186|
187| return false;
188| }
189|
190| function TestNotEmpty(element)
191| {
192| var untestedVal = element.value;
193|
194| if(untestedVal == null || untestedVal.length == 0)
195| {
196| return false;
197| }
198| else
199| {
200| return true;
201| }
202| }
203|
204| function TestEmail(element)
205| {
206| var untestedVal = element.value;
207| var regEX = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
208|
209| if(!untestedVal.match(regEX))
210| {
211| return false;
212| }
213| else
214| {
215| return true;
216| }
217| }
218|
219| function TestCapCorrect(elementAns, elementComp)
220| {
221| var Comp = elementComp.value;
222|
223| var CompLength = Comp.length;
224|
225| var CommaOne = Comp.search(",");
226| var CommaTwo = Comp.indexOf(",",CommaOne+1);
227|
228| var NumOne = parseInt(Comp.slice(0,CommaOne));
229| var NumTwo = parseInt(Comp.slice(CommaTwo+1,CompLength));
230| var Sign = Comp.slice(CommaOne+1,CommaTwo);
231|
232| var ValueEval = eval(NumOne + Sign + NumTwo);
233|
234| var ValueAns = elementAns.value;
235|
236| if(ValueEval == ValueAns)
237| {
238| return true;
239| }
240| else
241| {
242| return false;
243| }
244| }
245|
246| function TestNum(elementAns)
247| {
248| var ValueAns = elementAns.value;
249|
250| /*
251| This is the Regular Expression to match a number.
252| It looks for an optional sign ([-]), zero or more numbers
253| before a decimal point (\d*), an optional decimal point (\.?), and zero
254| or more numbers after the decimal point (\d*).
255| */
256| var regEX = /^[-]?\d*\.?\d*$/;
257|
258| ValueAns = ValueAns.toString();
259|
260| if(!ValueAns.match(regEX))
261| {
262| return false;
263| }
264| else
265| {
266| return true;
267| }
268| }
269|
270| function TestSelect(element)
271| {
272| if(element.selectedIndex == 0)
273| {
274| return false;
275| }
276| else
277| {
278| return true;
279| }
280| }