Skip to content

Commit dfc0252

Browse files
committed
Merge pull request #6 from qcode-software/quirks-validation
Add form validation plugin
2 parents 94857c1 + 938cc33 commit dfc0252

File tree

3 files changed

+245
-0
lines changed

3 files changed

+245
-0
lines changed

js/jquery.validation.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/* validation plugin
2+
- example usage -
3+
$('#my_form').validation({
4+
'#age': {
5+
check: function() {
6+
if ( isInt($(this).val()) ) {
7+
return true;
8+
} else {
9+
return false;
10+
}
11+
},
12+
message: "Age must be an integer."
13+
},
14+
'#name': $.validation.required,
15+
'#height': {
16+
check: function() {
17+
return $(this).val() > 10;
18+
},
19+
message: "Height must be greater than 10"
20+
}
21+
});
22+
23+
- also exposes -
24+
$.validation.showMessage($element, message);
25+
$.validation.hideMessage($element);
26+
$.validation.isPostcode(value);
27+
*/
28+
(function($) {
29+
// Closure variables
30+
var submitButtonSelector = "input[type=button],input[type=image],button[type!=reset][type!=button][type!=menu]";
31+
32+
// Plugin function
33+
$.fn.validation = function(options) {
34+
var $form = this;
35+
$.each(options, function(selector, fieldOptions) {
36+
// Validate each element on blur
37+
$form.find(selector)
38+
.on('blur', function() {
39+
$(this).val($(this).val().trim());
40+
if ( ! fieldOptions.check.call(this) ) {
41+
$.validation.showMessage($(this), fieldOptions.message);
42+
}
43+
})
44+
.on('focus', function() {
45+
$.validation.hideMessage($(this));
46+
});
47+
});
48+
$form.on('submit', function(event) {
49+
// Validate the entire form on submit
50+
$.each(options, function(selector, fieldOptions) {
51+
$form.find(selector).each(function() {
52+
$(this).val($(this).val().trim());
53+
if ( ! fieldOptions.check.call(this) ) {
54+
$.validation.showMessage($(this), fieldOptions.message);
55+
event.preventDefault();
56+
}
57+
});
58+
});
59+
// Disable submit buttons to prevent duplicate submission
60+
if ( ! event.isDefaultPrevented() ) {
61+
$(this).find(submitButtonSelector).attr('disabled', true);
62+
}
63+
});
64+
$form.on('reset', function() {
65+
$.each(options, function(selector, fieldOptions) {
66+
$form.find(selector).each(function() {
67+
$.validation.hideMessage($(this));
68+
});
69+
});
70+
});
71+
return this;
72+
}
73+
74+
// Utils
75+
$.validation = {
76+
required: {
77+
check: function() {
78+
return $(this).val() != "";
79+
},
80+
message: "This field is required."
81+
},
82+
isPostcode: function(string) {
83+
var re1 = /^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$/;
84+
var re2 = /^BFPO ?[0-9]+$/;
85+
return re1.test(string) || re2.test(string);
86+
},
87+
isInteger: function(string) {
88+
var re = /^\d+$/;
89+
return re.test(string);
90+
},
91+
isDate: function(string) {
92+
var re1 = /^\d{4}-\d{2}-\d{2}$/;
93+
var re2 = /^\d{2}\/\d{2}\/\d{4}$/;
94+
return re1.test(string) || re2.test(string);
95+
},
96+
isEmail: function(string) {
97+
var re = /^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)+$/;
98+
return re.test(string);
99+
},
100+
showMessage: function($element, message) {
101+
$element.qtip({
102+
content: message,
103+
position: {
104+
my: "left top",
105+
at: "top right",
106+
target: $element
107+
},
108+
show: {
109+
ready: true
110+
},
111+
hide: {
112+
event: 'focus',
113+
delay: 0
114+
}
115+
});
116+
},
117+
hideMessage: function($element) {
118+
$element.qtip('destroy');
119+
}
120+
}
121+
})(jQuery);
File renamed without changes.

qcode-ui-1.10.0.js renamed to qcode-ui-1.11.0.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8713,6 +8713,130 @@ function coalesce() {
87138713
}
87148714
})(window);
87158715

8716+
/* ==== jquery.validation.js ==== */
8717+
/* validation plugin
8718+
- example usage -
8719+
$('#my_form').validation({
8720+
'#age': {
8721+
check: function() {
8722+
if ( isInt($(this).val()) ) {
8723+
return true;
8724+
} else {
8725+
return false;
8726+
}
8727+
},
8728+
message: "Age must be an integer."
8729+
},
8730+
'#name': $.validation.required,
8731+
'#height': {
8732+
check: function() {
8733+
return $(this).val() > 10;
8734+
},
8735+
message: "Height must be greater than 10"
8736+
}
8737+
});
8738+
8739+
- also exposes -
8740+
$.validation.showMessage($element, message);
8741+
$.validation.hideMessage($element);
8742+
$.validation.isPostcode(value);
8743+
*/
8744+
(function($) {
8745+
// Closure variables
8746+
var submitButtonSelector = "input[type=button],input[type=image],button[type!=reset][type!=button][type!=menu]";
8747+
8748+
// Plugin function
8749+
$.fn.validation = function(options) {
8750+
var $form = this;
8751+
$.each(options, function(selector, fieldOptions) {
8752+
// Validate each element on blur
8753+
$form.find(selector)
8754+
.on('blur', function() {
8755+
$(this).val($(this).val().trim());
8756+
if ( ! fieldOptions.check.call(this) ) {
8757+
$.validation.showMessage($(this), fieldOptions.message);
8758+
}
8759+
})
8760+
.on('focus', function() {
8761+
$.validation.hideMessage($(this));
8762+
});
8763+
});
8764+
$form.on('submit', function(event) {
8765+
// Validate the entire form on submit
8766+
$.each(options, function(selector, fieldOptions) {
8767+
$form.find(selector).each(function() {
8768+
$(this).val($(this).val().trim());
8769+
if ( ! fieldOptions.check.call(this) ) {
8770+
$.validation.showMessage($(this), fieldOptions.message);
8771+
event.preventDefault();
8772+
}
8773+
});
8774+
});
8775+
// Disable submit buttons to prevent duplicate submission
8776+
if ( ! event.isDefaultPrevented() ) {
8777+
$(this).find(submitButtonSelector).attr('disabled', true);
8778+
}
8779+
});
8780+
$form.on('reset', function() {
8781+
$.each(options, function(selector, fieldOptions) {
8782+
$form.find(selector).each(function() {
8783+
$.validation.hideMessage($(this));
8784+
});
8785+
});
8786+
});
8787+
return this;
8788+
}
8789+
8790+
// Utils
8791+
$.validation = {
8792+
required: {
8793+
check: function() {
8794+
return $(this).val() != "";
8795+
},
8796+
message: "This field is required."
8797+
},
8798+
isPostcode: function(string) {
8799+
var re1 = /^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$/;
8800+
var re2 = /^BFPO ?[0-9]+$/;
8801+
return re1.test(string) || re2.test(string);
8802+
},
8803+
isInteger: function(string) {
8804+
var re = /^\d+$/;
8805+
return re.test(string);
8806+
},
8807+
isDate: function(string) {
8808+
var re1 = /^\d{4}-\d{2}-\d{2}$/;
8809+
var re2 = /^\d{2}\/\d{2}\/\d{4}$/;
8810+
return re1.test(string) || re2.test(string);
8811+
},
8812+
isEmail: function(string) {
8813+
var re = /^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)+$/;
8814+
return re.test(string);
8815+
},
8816+
showMessage: function($element, message) {
8817+
$element.qtip({
8818+
content: message,
8819+
position: {
8820+
my: "left top",
8821+
at: "top right",
8822+
target: $element
8823+
},
8824+
show: {
8825+
ready: true
8826+
},
8827+
hide: {
8828+
event: 'focus',
8829+
delay: 0
8830+
}
8831+
});
8832+
},
8833+
hideMessage: function($element) {
8834+
$element.qtip('destroy');
8835+
}
8836+
}
8837+
})(jQuery);
8838+
8839+
87168840
/* ==== resizeHeight.js ==== */
87178841
function resizeHeight(oObject) {
87188842

0 commit comments

Comments
 (0)