-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
201 lines (186 loc) · 6.09 KB
/
index.html
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jQuery Form Error Plugin Demo</title>
<link rel="stylesheet" type="text/css" media="screen" href="demo.css" />
<script src="lib/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="jquery.formError.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<h1>jQuery Form Error Plugin</h1>
<div id="content">
<form>
<table>
<tbody>
<tr>
<td>
<label for="name">* Name</label>
</td>
<td>
<input type="text" id="name" />
</td>
</tr>
<tr>
<td>
<label for="age">* Age</label>
</td>
<td>
<input type="text" id="age" />
</td>
</tr>
<tr>
<td>
<label for="email">* Email</label>
</td>
<td>
<input type="text" id="email" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<input id="addPersonButton" type="submit" value="Add person">
<br /><em class="note">* Required field</em>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
<div id="footer">
<a href="https://github.com/GarethElms/jQuery.formError">jQuery Form Error Plugin</a> By <a href="http://www.garethelms.org">Gareth Elms</a> 2012
</div>
</body>
<script type="text/javascript">
$(document).ready(
function()
{
$("#name").change( validate.controls.name);
$("#age").change( validate.controls.age);
$("#email").change( validate.controls.email);
$("#addPersonButton").click(
function( event)
{
event.preventDefault();
if( validate.all())
{
alert( "This dude is valid. I can continue");
}
});
});
var validate =
(function()
{
var _regex =
{
emailAddressIsValid:
function( emailAddress)
{
// http://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex
var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
return pattern.test( emailAddress);
},
isWholeNumber:
function( age)
{
var pattern = new RegExp( /^\d+$/);
return pattern.test( age);
}
};
var all =
function()
{
var invalidControls = [];
for( var controlValidationMethod in validate.controls)
{
if( ! validate.controls[controlValidationMethod].call( $("#" + controlValidationMethod)))
{
invalidControls.push( controlValidationMethod);
}
}
if( invalidControls.length > 0)
{
// Set focus on the first erroneous control
$("#" + invalidControls[0]).focus();
}
return invalidControls.length == 0;
};
var controls =
{
name:
function()
{
var $input = $(this);
var isValid = true;
if( $input.val() === "")
{
$input.formError( "Please enter a name");
isValid = false;
}
else if( $input.val().length > 15)
{
$input.formError( "Name cannot be greater than 15 characters long");
isValid = false;
}
else
{
// Valid, remove any existing form error message for this input
$input.formError( {remove:true});
}
return isValid;
},
age:
function()
{
var $input = $(this);
var isValid = true;
if( $input.val() === "")
{
$input.formError( "Please enter the person's age");
isValid = false;
}
else if( ! _regex.isWholeNumber( $input.val()))
{
$input.formError( "Please enter a valid age eg; 21");
isValid = false;
}
else
{
// Valid, remove any existing form error message for this input
$input.formError( {remove:true});
}
return isValid;
},
email:
function()
{
var $input = $(this);
var isValid = true;
if( $input.val() === "")
{
$input.formError( "Please enter an email address");
isValid = false;
}
else if( ! _regex.emailAddressIsValid( $input.val()))
{
$input.formError( "Please enter a valid email address<br /> eg; [email protected]");
isValid = false;
}
else
{
// Valid, remove any existing form error message for this input
$input.formError( {remove:true});
}
return isValid;
}
};
return {
"all": all,
"controls": controls};
})();
</script>
</html>