-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_validation.js
151 lines (135 loc) · 4.94 KB
/
form_validation.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//signup form validation
$(function(){
$('#errorname').hide();
$('#errorusername').hide();
$('#erroremail').hide();
$('#errorpass').hide();
$('#errorconpass').hide();
var error_name = false;
var error_username = false;
var error_email = false;
var error_pass = false;
var error_conpass = false;
$('#form_name').focusout(function(){
checkName();
});
$('#form_username').focusout(function(){
checkUserName();
});
$('#form_email').focusout(function(){
checkEmail();
});
$('#form_pwd').focusout(function(){
checkPass();
});
$('#form_conpwd').focusout(function(){
checkConPass();
});
// check name
function checkName(){
var name = $('#form_name').val();
var pattern = new RegExp(/^[a-zA-Z ]{5,30}$/);
if (!pattern.test(name)) {
$('#errorname').html('Should be between 5-30 contains only space');
$('#errorname').show(300);
error_name = true;
}
else {
$('#errorname').hide(400);
}
}
// check username
function checkUserName(){
var username = $('#form_username').val();
var pattern = new RegExp(/^[a-zA-Z0-9._]{6,20}$/);
if (!pattern.test(username)) {
$('#errorusername').html('Should be between 6-20 contains only alphabets numbers . _');
$('#errorusername').show(300);
error_username = true;
}
else {
$('#errorusername').hide(400);
}
}
//check email
function checkEmail(){
var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
if (pattern.test($("#form_email").val())) {
$('#erroremail').hide(400);
}
else {
$('#erroremail').html('Invalid email address');
$('#erroremail').show(300);
error_email = true;
}
}
// check password
function checkPass(){
var password = $('#form_pwd').val();
var pattern = new RegExp(/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/);
if (!pattern.test(password)) {
$('#errorpass').html('Should be at least a uppercase,lowercase,number,special characters and minimum length 8');
$('#errorpass').show(300);
error_pass = true;
}
else {
$('#errorpass').hide(400);
}
}
// check confirm password
function checkConPass(){
var password = $('#form_pwd').val();
var con_pass = $('#form_conpwd').val();
if (password != con_pass) {
$('#errorconpass').html('Password not match');
$('#errorconpass').show(300);
error_conpass = true;
}
else {
$('#errorconpass').hide(400);
}
}
//form submit
$('#reg_form').submit(function(){
error_name = false;
error_username = false;
error_email = false;
error_pass = false;
error_conpass = false;
checkName();
checkUserName();
checkEmail();
checkPass();
checkConPass();
if (error_name == false && error_username == false && error_email==false && error_pass == false && error_conpass == false) {
return true;
}
else {
return false;
}
});
});
// This is html form
// <form id="reg_form" action="" method="">
// <div class="form-group">
// <span class="signup-error" id="errorname">Name length must be 5! No special chars allowed</span>
// <input type="text" class="form-control" id="form_name" placeholder="Full Name" name="name">
// </div>
// <div class="form-group">
// <span class="signup-error" id="errorusername">username al least 5 chars</span>
// <input type="text" class="form-control" id="form_username" placeholder="Username" name="username">
// </div>
// <div class="form-group">
// <span class="signup-error" id="erroremail">Invalid email format</span>
// <input type="email" class="form-control" id="form_email" placeholder="Email" name="email">
// </div>
// <div class="form-group">
// <span class="signup-error" id="errorpass">Pasword must 6 chars</span>
// <input type="password" class="form-control" id="form_pwd" placeholder="Password" name="pass">
// </div>
// <div class="form-group">
// <span class="signup-error" id="errorconpass">Password not match</span>
// <input type="password" class="form-control" id="form_conpwd" placeholder="Confirm password">
// </div>
// <button type="submit" class="btn btn-default" name="submit">Signup</button>
// </form>