-
Notifications
You must be signed in to change notification settings - Fork 1
/
regform.js
56 lines (41 loc) · 1.34 KB
/
regform.js
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
function clearErrors() {
errors = document.getElementsByClassName('formerror');
for (let item of errors) {
item.innerHTML = "";
}
}
function seterror(id, error) {
//sets error inside tag of id
element = document.getElementById(id);
element.getElementsByClassName('formerror')[0].innerHTML = error;
}
function validateForm() {
var returnval = true;
clearErrors();
//perform validation and if validation fails, set the value of returnval to false
var name = document.forms['myForm']["myname"].value;
if (name.length < 5) {
seterror("name", "*Length of name is too short");
returnval = false;
}
if (name.length == 0) {
seterror("name", "*Length of name cannot be zero!");
returnval = false;
}
var email = document.forms['myForm']["myemail"].value;
if (email.length > 15) {
seterror("email", "*Email length is too long");
returnval = false;
}
var phone = document.forms['myForm']["mynumber"].value;
if (phone.length != 10) {
seterror("phone", "*Phone number should be of 10 digits!");
returnval = false;
}
var address = document.forms['myForm']["myadress"].value;
if (address.length < 6) {
seterror("address", "*too small address");
returnval = false;
}
return returnval;
}