forked from LeadSpend/jquery-validate-leadspend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.validate.leadspend.js
More file actions
160 lines (138 loc) · 4.9 KB
/
jquery.validate.leadspend.js
File metadata and controls
160 lines (138 loc) · 4.9 KB
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
/**
* LeadSpend jQuery Validate integration V1.0
*
* Adds functions to the LeadSpend namespace to aid with integration into jQuery
* validate.
*
*
* Dependencies:
* LeadSpendValidate.js V2.1
* jQuery Validate: http://jzaefferer.github.com/jquery-validation/jquery.validate.js
* jQuery
*/
/**
* getEmailValidity is a function designed for use with the jQuery Validate
* plugin as a custom rule. This function calls the asynchronous
* LeadSpend.validate function and triggers the event "LeadSpendCallback" when
* the validity is returned.
*
* @param {string} address This is the email address to be verified.
* @returns {boolean} Returns whether the email address is valid or not, returns
* false if the LeadSpend result has not yet been returned.
*/
LeadSpend.getEmailValidity = function(address){
//if the form has not been validated, assume the response is not valid
if (typeof LeadSpend.allowAddress == 'undefined'){
//values are added to the LeadSpend namespace to avoid collisions
LeadSpend.allowAddress = false;
LeadSpend.lastVerifiedAddress = address;
}
//if this email address has not changed, don't make a duplicate call to LS
else if(LeadSpend.lastVerifiedAddress == address){
return LeadSpend.allowAddress;
}
//if the validity is still pending, the form is not valid
else if(LeadSpend.validityPending){
return false;
}
LeadSpend.validityPending = true;
LeadSpend.lastVerifiedAddress = address;
timeout = 5;
//call the LeadSpend validate function with a custom callback
LeadSpend.validate(address, timeout, function(validity){
LeadSpend.validity = validity;
LeadSpend.allowAddress = validity.result == "unknown" || validity.result == "verified";
LeadSpend.validityPending = false;
//now that the result has been obtained, revalidate the field
LeadSpend.reValidate();
});
return false;
}
/**
* getEmailValidity is a function designed for use with the jQuery Validate
* plugin as part of a custom invalidHandler. If this user has pressed submit
* while the email address was being validated, this is stored as
* LeadSpend.submitAttempted.
*
* @param {form} form This is the form object used by jQuery
* validate.
* @param {validator} validator This is the validator object used by jQuery
* validate.
*
*/
LeadSpend.invalidHandler = function(form, validator){
LeadSpend.validator = validator;
LeadSpend.formID = form.target.id;
var errors = validator.numberOfInvalids();
if (errors == 1 && LeadSpend.validityPending){
LeadSpend.submitAttempted = true;
}
}
/**
* reValidate is triggered after the email result has been returned, and is
* used to update the validity of the email field.
*
*/
LeadSpend.reValidate = function(){
$("form").validate().element(".LeadSpendEmail");
//submit the form if a submit was previously blocked by a validating email
if (LeadSpend.submitAttempted && LeadSpend.validator.numberOfInvalids() == 0){
document.forms[LeadSpend.formID].submit.click();
}
//otherwise they have made the form invalid since pressing submit, so they
//need to keep changing stuff
else if (LeadSpend.validator.numberOfInvalids >= 1){
LeadSpend.submitAttempted = false;
}
}
/**
* setMessages is used to set custom messages for the two possible error
* messages that can be returned by LeadSpend: one for validiation in progress,
* and one for an invalid email.
*
*/
LeadSpend.setEmailMessages = function(field){
lsRules = $("form").validate().settings.rules[field.name].LeadSpendEmail;
//set default LeadSpend messages for various stages of invalid addresses
if(typeof lsRules.validityPendingMessage == "string"){
LeadSpend.validityPendingMessage = lsRules.validityPendingMessage;
}
else if(typeof lsRules.validityPendingMessage == "function"){
LeadSpend.validityPendingMessage = messages.validityPending(LeadSpend.validity);
}
else{
LeadSpend.validityPendingMessage = "Validating...";
}
if(typeof lsRules.denyAddressMessage == "string"){
LeadSpend.denyAddressMessage = lsRules.denyAddressMessage;
}
else if(typeof lsRules.denyAddressMessage == "function"){
LeadSpend.denyAddressMessage = lsRules.denyAddressMessage(LeadSpend.validity);
}
else{
LeadSpend.denyAddressMessage = LeadSpend.defaultDenyAddressMessage(LeadSpend.validity);
}
}
LeadSpend.defaultDenyAddressMessage = function(validity){
if (typeof validity != "undefined"){
return "Email address is "+validity.result+".";
}
return "Invalid address.";
}
/**
* getMessage is a function designed for use with the jQuery Validate
* plugin as part of a custom message function. Returns a custom error message
* for the two possible error conditions: email validation pending and invalid
* email address.
*
*/
LeadSpend.getMessage = function(field){
LeadSpend.setEmailMessages(field);
//return the appropriate message for the current state
if (LeadSpend.validityPending){
return LeadSpend.validityPendingMessage;
}
else{
return LeadSpend.denyAddressMessage;
}
}