forked from cbsandeep10/IMathAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateform.js
More file actions
executable file
·59 lines (59 loc) · 1.78 KB
/
validateform.js
File metadata and controls
executable file
·59 lines (59 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//By Martin Honnen
//taken from http://www.faqts.com/knowledge_base/view.phtml/aid/1756/fid/129
function validateForm (form) {
for (var e = 0; e < form.elements.length; e++) {
var el = form.elements[e];
if (typeof el.type == "undefined" || typeof el.name == "undefined") {
continue;
}
if (el.type == 'text' || el.type == 'textarea' ||
el.type == 'password' || el.type == 'file' ) {
if (el.value == '') {
//alert('Please fill out all text field ' + el.name);
alert('Please complete all fields');
el.focus();
return false;
}
}
else if (el.type.indexOf('select') != -1) {
if (el.selectedIndex == -1) {
//alert('Please select a value of the select field ' + el.name);
alert('Please complete all fields');
el.focus();
return false;
}
}
else if (el.type == 'radio') {
var group = form[el.name];
var checked = false;
if (!group.length)
checked = el.checked;
else
for (var r = 0; r < group.length; r++)
if ((checked = group[r].checked))
break;
if (!checked) {
//alert('Please check one of the radio buttons ' + el.name);
alert('Please complete all fields');
el.focus();
return false;
}
}
else if (el.type == 'checkbox') {
var group = form[el.name];
if (group.length) {
var checked = false;
for (var r = 0; r < group.length; r++)
if ((checked = group[r].checked))
break;
if (!checked) {
//alert('Please check one of the checkboxes ' + el.name);
alert('Please complete all fields');
el.focus();
return false;
}
}
}
}
return true;
}