-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.validation.engine.js
71 lines (66 loc) · 2.01 KB
/
forms.validation.engine.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
/*
* A Standard Set of Rules and Descriptive Error Messages for Building a Basic
* Validation Engine.
*
*/
var errMsg = {
// Check for when a specified field is required
required :
{
msg : 'This field is required.',
test : function( elem, load ) {
// Make sure that there is no text was entered in the field
// and we aren't checking on page load (showing 'field required'
// messages would be annoying on page load)
return elem.value.length > 0 || load || elem.value == elem.defaultValue;
}
},
// Make sure that the field is a valid email address
email :
{
msg : 'Not a valid email address.',
test : function( elem ) {
// Make sure that something was entered and that it looks like
// an email address
return !elem.value || /^[a-z0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test( elem.value );
}
},
// Make sure the field is a phone number
phone :
{
msg : 'Not a valid phone number.',
test : function( elem ) {
// Check to see if we have something that looks like
// a valid phone 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 ) {
elem.value = "(" + m[1] + ") " + m[2] + "-" + m[3];
}
return !elem.value || m;
}
},
// Make sure that the field is a valid MM/DD/YYYY date
date :
{
msg : 'Not a valid date.',
test : function( elem ) {
// Make sure that something 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);
}
},
// Make sure that the field is a valid URL
url :
{
msg : 'Not a valid URL.',
test : function( elem ) {
// Make sure that some text was entered, and that it's
// not the default http:// text
return !obj.value || obj.value == 'http://' ||
// Make sure that it looks like a valid URL
/^https?:\/\/([a-z0-9-]+\.)+[a-z0-9]{2,4}.*$/.test( obj.value );
}
}
};