-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.validation.helpers.js
230 lines (201 loc) · 6.39 KB
/
forms.validation.helpers.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* @function checkRequired
*
* Helper Function for Checking to see if an input element has had information entered
*
* @param elem { HTML Element }
*
*/
function checkRequired ( elem ) {
if ( elem.type == 'checkbox' || elem.type == 'radio' ) {
return getInputsByName( elem.name ).numChecked;
} else {
return elem.value.length > 0 && elem.value != elem.defaultValue;
}
}
/*
* @function checkRequired
*
* Helper Function for Finding All Input Elements that Have a Specified Name
* (good for finding and dealing with checkboxes or radio buttons)
*
* @param name { String }
*
*/
function getInputsByName( name ) {
// The array of input elements that will be matched
var results = [];
// Keep track of how many of them were checked
results.numChecked = 0;
// Find all the input elements in the document
var input = document.getElementsByTagName( 'input' );
for ( var i = 0; i < input.length; i++ ) {
// Find all the fields that have the specified name
if ( input[ i ].name == name ) {
results.push( input[ i ] );
// Remember how many if the fields were checked
if ( input[ i ].checked ) {
results.numChecked++;
}
}
}
// Return the set of matched fields
return results;
}
/*
* @function checkEmail
*
* Checking Whether a Specific Input Element Has an E-mail Address in It
*
* @param elem { HTML Element }
*
*/
function checkEmail( elem ) {
// Make sure that something was entered and that it looks like a valid email
return elem.value == '' || /^[a-z0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test( elem.value );
}
/*
* @function checkURL
*
* Checking Whether an Input Element Has a URL in It
*
* @param elem { HTML Element }
*
*/
function checkURL( elem ) {
// Make sure some text was entered, and it's not default http:// text
return elem.value == '' || !elem.value == 'http://' ||
// Make sure that it looks like a valid URL
/^https?:\/\/([a-z0-9-]+\.)+[a-z0-9]{2,4}.*$/.test( elem.value );
}
/*
* @function checkURL
*
* Checking Whether a Field Contains a Phone Number
*
* @param elem { HTML Element }
*
*/
function checkPhone( elem ) {
// Check to see if we have something that looks like a valid number
var m = /(\d{3}).*(\d{3}).*(\d{4})/.exec( elem.value );
// If it is, seemingly, valid - force it into the specific
// format that we desire: (123) 456-7890
if ( m !== null ) {
elem.value = '(' + m[ 1 ] + ') ' + m[ 2 ] + '-' + m[ 3 ];
}
return elem.value == '' || m !== null;
}
/*
* @function checkDate
*
* Checking Whether a Field Has a Date in It
*
* @param elem { HTML Element }
*
*/
function checkDate( elem ) {
// Make sure that sth is entered, and that it looks like
// a valid MM/DD/YYYY date
return !elem.value || /^\d{2}\/\d{2}\/\d{2,4}$/.test(elem.value);
}
/*
* @function validateForm
*
* Functions for Performing Form Validation and Triggering the Display of Error Messages
*
* @param form { HTML Element }
* @param load { Boolean }
*
*/
// A function for validating all fields within a form.
// The form argument should be a reference to a form element
// The load argument should be a boolean referring to the fact that
// the validation function is being run on page load, versus dynamically
function validateForm( form, load ) {
var valid = true;true
// Go through all the field elements in form
// form.elements is an array of all fields in a form
for ( var i = 0; i < form.elements.length; i++ ) {
// Hide any error message, if they are being shown
hideErrors( form.elements[ i ] );
// Check to see if the field contains valid contents, or not
if ( ! validateField( form.elements[ i ], load ) ) {
valid = false;
}
}
// Return false if a field does not have valid contents
// true if all fields are valid
return valid;
}
// Validate a single field's contents
function validateField( elem, load ) {
var errors = [];
// Go through all the possible validation techniques
for ( var name in errMsg ) {
// See if the field has the class specified by the error type
var re = new RegExp( "(^|\\s)" + name + "(\\s|$)" );
// Check to see if the element has the class and that it passes the validation test
if ( re.test( elem.className ) && !errMsg[ name ].test( elem, load ) ) {
// if it fails the validation, add the error message to list
errors.push( errMsg[ name ].msg );
}
}
// Show the error messages, if they exist
if ( errors.length ) {
showErrors( elem, errors );
}
// Return false if the field fails any of the validation routines
return errors.length > 0;
}
// Hide any validation error messages that are currently shown
function hideErrors( elem ) {
// Find the next element after the current field
var next = elem.nextSibling;
// If the next element is a ul and has a class of errors
if ( next && next.nodeName == 'UL' && next.className == 'errors' ) {
// Remove it (which is our means of hiding)
elem.parentNode.removeChild( next );
}
}
// Show a set of errors messages for a specific field within a form
function showErrors( elem, errors ) {
// Find the next element after the field
var next = elem.nextSibling;
// If the field isn't one of our special error-holders
if ( next && ( next.nodeName != 'UL' || next.className != 'errors' ) ) {
// We need to make one instead
next = document.createElement( 'ul' );
next.className = 'errors';
// and then insert into the correct place in the DOM
elem.parentNode.insertBefore( next, elem.nextSibling );
}
// Now that we have a reference to the rror holder UL
// We then loop through all the error messages
for ( var i = 0; i < errors.length; i++ ) {
// Create a new li wrapper for each
var li = document.createElement( 'li' );
li.innerHTML = errors[ i ];
// And insert into DOM
next.appendChild( li );
}
}
// Waiting Until a Form is Submitted to Run the Form Validation Function
function watchForm( form ) {
addEvent( form, 'submit', function() {
// make sure that the form's contents validate correctly
return validateForm( form );
});
}
// Watching Fields for a Change Before Running Any Field Validation Functions
function watchFields( form ) {
// Go through all the field elements in form
for ( var i = 0; i < form.elements.length; i++ ) {
// and attach a 'change' event handler (which watches for a user
// to lose focus of an input element)
addEvent( form.elements[ i ], 'change', function(){
// Once the focus has been lost, re-validate the field
return validateField( this );
} );
}
}