Skip to content

Commit e27fb7e

Browse files
committed
Version 1.4.0
- Added destroy method Note - node now refers to a jQuery object instead of the DOM object.
1 parent 7a76e84 commit e27fb7e

File tree

3 files changed

+145
-85
lines changed

3 files changed

+145
-85
lines changed

html5-form-validation.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"validation",
1010
"input"
1111
],
12-
"version": "1.3.5",
12+
"version": "1.4.0",
1313
"author": {
1414
"name": "Tom Bertrand",
1515
"url": "http://www.runningcoder.org/jqueryvalidation/"

jquery.validation.js

Lines changed: 142 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* jQuery Form Validation
33
*
44
* @author Tom Bertrand
5-
* @version 1.3.5 (2014-09-10)
5+
* @version 1.4.0 (2014-09-28)
66
*
77
* @copyright
88
* Copyright (C) 2014 RunningCoder.
@@ -72,25 +72,25 @@
7272
* Error messages
7373
*/
7474
var _messages = Object.preventExtensions({
75-
'default': '$ contain error(s).',
76-
'NOTEMPTY': '$ must not be empty.',
77-
'NUMERIC': '$ must be numeric.',
78-
'STRING': '$ must be a string.',
79-
'NOSPACE': '$ must not contain spaces.',
80-
'TRIM': '$ must not start or end with space character.',
81-
'MIXED': '$ must be letters or numbers (no special characters).',
82-
'DATE': '$ is not a valid with format YYYY-MM-DD.',
83-
'EMAIL': '$ is not valid.',
84-
'URL': '$ is not valid.',
85-
'PHONE': '$ is not a valid phone number.',
86-
//'INARRAY': '$ is not a valid option.',
87-
'<': '$ must be less than % characters.',
88-
'<=': '$ must be less or equal to % characters.',
89-
'>': '$ must be greater than % characters.',
90-
'>=': '$ must be greater or equal to % characters.',
91-
'==': '$ must be equal to %',
92-
'!=': '$ must be different than %'
93-
}),
75+
'default': '$ contain error(s).',
76+
'NOTEMPTY': '$ must not be empty.',
77+
'NUMERIC': '$ must be numeric.',
78+
'STRING': '$ must be a string.',
79+
'NOSPACE': '$ must not contain spaces.',
80+
'TRIM': '$ must not start or end with space character.',
81+
'MIXED': '$ must be letters or numbers (no special characters).',
82+
'DATE': '$ is not a valid with format YYYY-MM-DD.',
83+
'EMAIL': '$ is not valid.',
84+
'URL': '$ is not valid.',
85+
'PHONE': '$ is not a valid phone number.',
86+
//'INARRAY': '$ is not a valid option.',
87+
'<': '$ must be less than % characters.',
88+
'<=': '$ must be less or equal to % characters.',
89+
'>': '$ must be greater than % characters.',
90+
'>=': '$ must be greater or equal to % characters.',
91+
'==': '$ must be equal to %',
92+
'!=': '$ must be different than %'
93+
}),
9494
_extendedMessages = false;
9595

9696
/**
@@ -113,45 +113,45 @@
113113
*
114114
* @link http://www.runningcoder.org/jqueryvalidation/documentation/
115115
*/
116-
var _options = {
117-
submit: {
118-
settings: {
119-
form: null,
120-
display: "inline",
121-
insertion: "append",
122-
allErrors: false,
123-
trigger: "click",
124-
button: "input[type='submit']",
125-
errorClass: "error",
126-
errorListClass: "error-list",
127-
inputContainer: null,
128-
clear: "focusin",
129-
scrollToError: false
130-
},
131-
callback: {
132-
onInit: null,
133-
onValidate: null,
134-
onError: null,
135-
onBeforeSubmit: null,
136-
onSubmit: null,
137-
onAfterSubmit: null
138-
}
139-
},
140-
dynamic: {
141-
settings: {
142-
trigger: null,
143-
delay: 300
144-
},
145-
callback: {
146-
onSuccess: null,
147-
onError: null,
148-
onComplete: null
149-
}
150-
},
151-
messages: {},
152-
labels: {},
153-
debug: false
154-
};
116+
var _options = {
117+
submit: {
118+
settings: {
119+
form: null,
120+
display: "inline",
121+
insertion: "append",
122+
allErrors: false,
123+
trigger: "click",
124+
button: "input[type='submit']",
125+
errorClass: "error",
126+
errorListClass: "error-list",
127+
inputContainer: null,
128+
clear: "focusin",
129+
scrollToError: false
130+
},
131+
callback: {
132+
onInit: null,
133+
onValidate: null,
134+
onError: null,
135+
onBeforeSubmit: null,
136+
onSubmit: null,
137+
onAfterSubmit: null
138+
}
139+
},
140+
dynamic: {
141+
settings: {
142+
trigger: null,
143+
delay: 300
144+
},
145+
callback: {
146+
onSuccess: null,
147+
onError: null,
148+
onComplete: null
149+
}
150+
},
151+
messages: {},
152+
labels: {},
153+
debug: false
154+
};
155155

156156
/**
157157
* @private
@@ -191,8 +191,11 @@
191191
*/
192192
var Validation = function (node, options) {
193193

194-
var errors = [];
195-
window.Validation.hasScrolled = false;
194+
var errors = [],
195+
delegateSuffix = ".vd", // validation.delegate
196+
resetSuffix = ".vr"; // validation.resetError
197+
198+
window.Validation.hasScrolled = false;
196199

197200
/**
198201
* Extends user-defined "message" into the default Validation "_message".
@@ -352,10 +355,9 @@
352355
return false;
353356
}
354357

355-
var namespace = ".vd", // validation.delegate
356-
event = options.dynamic.settings.trigger + namespace;
358+
var event = options.dynamic.settings.trigger + delegateSuffix;
357359
if (options.dynamic.settings.trigger !== "focusout") {
358-
event += " change" + namespace + " paste" + namespace;
360+
event += " change" + delegateSuffix + " paste" + delegateSuffix;
359361
}
360362

361363
$.each(
@@ -419,9 +421,9 @@
419421
// {debug}
420422
options.debug && window.Debug.log({
421423
'node': node,
422-
'function': 'delegateDynamicValidation()',
423-
'arguments': 'node.find(' + options.submit.settings.button + ')',
424-
'message': 'ERROR - ' + options.submit.settings.button + ' not found'
424+
'function': 'delegateValidation()',
425+
'arguments': '{button: ' + options.submit.settings.button + '}',
426+
'message': 'ERROR - node.find("' + options.submit.settings.button + '") not found'
425427
});
426428
// {/debug}
427429

@@ -524,9 +526,9 @@
524526

525527
matches = inputName.replace(/]$/, '').split(/]\[|[[\]]/g),
526528
inputShortName = window.Validation.labels[inputName] ||
527-
options.labels[inputName] ||
528-
$(input).attr(_data.label) ||
529-
matches[matches.length - 1],
529+
options.labels[inputName] ||
530+
$(input).attr(_data.label) ||
531+
matches[matches.length - 1],
530532

531533
validationArray = $(input).attr(_data.validation),
532534
validationMessage = $(input).attr(_data.validationMessage),
@@ -744,9 +746,8 @@
744746
throw [options.messages[operator].replace(' characters', ''), compared];
745747
}
746748

747-
// Compare numeric value
748749
} else {
749-
750+
// Compare numeric value
750751
if (!value || isNaN(value) || !eval(value + operator + parseFloat(compared))) {
751752
throw [options.messages[operator].replace(' characters', ''), compared];
752753
}
@@ -878,7 +879,7 @@
878879

879880
if (options.submit.settings.display === "inline" ||
880881
(options.submit.settings.display === "block" && !errorContainer.find('[' + _data.errorList + ']')[0])
881-
) {
882+
) {
882883
if (options.submit.settings.insertion === 'append') {
883884
errorContainer.append(html);
884885
} else if (options.submit.settings.insertion === 'prepend') {
@@ -896,15 +897,14 @@
896897
input = groupInput;
897898
}
898899

899-
var namespace = ".vr", //validation.resetError
900-
event = "coucou" + namespace;
900+
var event = "coucou" + resetSuffix;
901901
if (options.submit.settings.clear) {
902-
event += " " + options.submit.settings.clear + namespace
902+
event += " " + options.submit.settings.clear + resetSuffix
903903
}
904904
if (options.dynamic.settings.trigger) {
905-
event += " " + options.dynamic.settings.trigger + namespace;
905+
event += " " + options.dynamic.settings.trigger + resetSuffix;
906906
if (options.dynamic.settings.trigger !== "focusout") {
907-
event += " change" + namespace + " paste" + namespace;
907+
event += " change" + resetSuffix + " paste" + resetSuffix;
908908
}
909909
}
910910

@@ -999,9 +999,7 @@
999999
}
10001000
}
10011001

1002-
//$._data( input[0], "events" );
1003-
input.trigger('coucou.vr');
1004-
1002+
input.trigger('coucou' + resetSuffix);
10051003
}
10061004

10071005
}
@@ -1031,6 +1029,38 @@
10311029

10321030
}
10331031

1032+
/**
1033+
* Submits the form once it succeeded the validation process.
1034+
* Note:
1035+
* - This function will be overridden if "options.submit.settings.onSubmit" is defined
1036+
* - The node can't be submitted by jQuery since it has been disabled, use the form native submit function instead
1037+
*
1038+
* @returns {boolean}
1039+
*/
1040+
function destroy () {
1041+
1042+
var event = options.submit.settings.trigger + delegateSuffix;
1043+
1044+
if (options.dynamic.settings.trigger) {
1045+
event += " " + options.dynamic.settings.trigger + delegateSuffix;
1046+
if (options.dynamic.settings.trigger !== "focusout") {
1047+
event += " change" + delegateSuffix + " paste" + delegateSuffix;
1048+
}
1049+
}
1050+
1051+
$.each(
1052+
node.find('[' + _data.validation + '],[' + _data.regex + ']'),
1053+
function (i, v) {
1054+
$(v).unbind(event);
1055+
}
1056+
);
1057+
1058+
//delete window.Validation.form[node.selector];
1059+
1060+
return true;
1061+
1062+
}
1063+
10341064
/**
10351065
* @private
10361066
* Helper to get the value of an regular, radio or chackbox input
@@ -1206,7 +1236,13 @@
12061236
* @public
12071237
* Remove all errors
12081238
*/
1209-
resetErrors: resetErrors
1239+
resetErrors: resetErrors,
1240+
1241+
/**
1242+
* @public
1243+
* Destroy the Validation instance
1244+
*/
1245+
destroy: destroy
12101246

12111247
};
12121248

@@ -1429,6 +1465,30 @@
14291465
return;
14301466
}
14311467

1468+
if (options === "destroy") {
1469+
1470+
if (!window.Validation.form[node.selector]) {
1471+
1472+
// {debug}
1473+
window.Debug.log({
1474+
'node': node,
1475+
'function': '$.validate("destroy")',
1476+
'arguments': '',
1477+
'message': 'Unable to destroy "' + node.selector + '", perhaps it\'s already destroyed?'
1478+
});
1479+
1480+
window.Debug.print();
1481+
// {/debug}
1482+
1483+
return;
1484+
}
1485+
1486+
window.Validation.form[node.selector].destroy();
1487+
1488+
return;
1489+
1490+
}
1491+
14321492
return node.each(function () {
14331493
window.Validation.form[node.selector] = new Validation($(this), options);
14341494
});

0 commit comments

Comments
 (0)