Skip to content

Commit 49b51cf

Browse files
author
Cedric Dugas
committed
added function type in language validation
1 parent 1197ae8 commit 49b51cf

26 files changed

+190
-36
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,17 @@ Validates the element's value to a predefined list of regular expressions.
350350

351351
Please refer to the section *Custom Regex* for a list of available regular expressions.
352352

353+
354+
### custom[function_name]
355+
356+
Validates the element's value to a predefined function included in the language file (compared to funCall that can be anywhere in your application),
357+
358+
```html
359+
<input value="someone@nowhere.com" class="validate[required,custom[requiredInFunction]]" type="text" name="email" id="email" />
360+
```
361+
362+
Please refer to the section *Custom Regex* for a list of available regular expressions.
363+
353364
### funcCall[methodName]
354365

355366
Validates a field using a third party function call. If a validation error occurs, the function must return an error message that will automatically show in the error prompt.

demos/demoValidators.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@
255255
Note* Both fields must have the same name
256256
</label>
257257
</fieldset>
258-
258+
<fieldset>
259+
<label>
260+
<span>Field is required with function : </span>
261+
<input value="" class="validate[required, custom[requiredInFunction]] text-input" type="text" name="req3" id="req3" />
262+
</label>
263+
</fieldset>
259264
<fieldset>
260265
<legend>
261266
Date Time Range<br />

js/jquery.validationEngine.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
hide: function() {
182182
var form = $(this).closest('form');
183183
if(form.length == 0)
184-
return this;
184+
return this;
185185
var options = form.data('jqv');
186186
var closingtag;
187187
if($(this).is("form")) {
@@ -689,28 +689,28 @@
689689
}
690690

691691
if(rule["regex"]) {
692-
var ex=rule.regex;
693-
if(!ex) {
694-
alert("jqv:custom regex not found - "+customRule);
695-
return;
696-
}
697-
var pattern = new RegExp(ex);
698-
699-
if (!pattern.test(field.val()))
700-
return options.allrules[customRule].alertText;
701-
} else if(rule["function"]) {
702-
fn = rule["function"]
703-
704-
if (typeof(fn) !== "function") {
705-
alert("jqv:custom parameter 'function' is no function - "+customRule);
706-
return;
707-
}
708-
709-
if (!fn(field, rules, i, options))
710-
return options.allrules[customRule].alertText;
692+
var ex=rule.regex;
693+
if(!ex) {
694+
alert("jqv:custom regex not found - "+customRule);
695+
return;
696+
}
697+
var pattern = new RegExp(ex);
698+
699+
if (!pattern.test(field.val())) return options.allrules[customRule].alertText;
700+
701+
} else if(rule["func"]) {
702+
fn = rule["func"]
703+
704+
if (typeof(fn) !== "function") {
705+
alert("jqv:custom parameter 'function' is no function - "+customRule);
706+
return;
707+
}
708+
709+
if (!fn(field, rules, i, options))
710+
return options.allrules[customRule].alertText;
711711
} else {
712-
alert("jqv:custom type not allowed "+customRule);
713-
return;
712+
alert("jqv:custom type not allowed "+customRule);
713+
return;
714714
}
715715
},
716716
/**
@@ -1042,7 +1042,7 @@
10421042
var extraDataDynamic = rule.extraDataDynamic;
10431043

10441044
if (!extraData)
1045-
extraData = "";
1045+
extraData = "";
10461046

10471047
if (extraDataDynamic) {
10481048
var tmpData = [];
@@ -1075,7 +1075,7 @@
10751075
// build the loading prompt
10761076
var loadingText = rule.alertTextLoad;
10771077
if (loadingText)
1078-
methods._showPrompt(field, loadingText, "load", true, options);
1078+
methods._showPrompt(field, loadingText, "load", true, options);
10791079
},
10801080
error: function(data, transport) {
10811081
methods._ajaxError(data, transport);
@@ -1102,33 +1102,33 @@
11021102
if (options.allrules[msg]) {
11031103
var txt = options.allrules[msg].alertText;
11041104
if (txt)
1105-
msg = txt;
1105+
msg = txt;
11061106
}
11071107
}
11081108
else
1109-
msg = rule.alertText;
1109+
msg = rule.alertText;
11101110

11111111
methods._showPrompt(errorField, msg, "", true, options);
11121112
} else {
11131113
if (options.ajaxValidCache[errorFieldId] !== undefined)
1114-
options.ajaxValidCache[errorFieldId] = true;
1114+
options.ajaxValidCache[errorFieldId] = true;
11151115

11161116
// resolves the msg prompt
11171117
if(msg) {
11181118
if (options.allrules[msg]) {
11191119
var txt = options.allrules[msg].alertTextOk;
11201120
if (txt)
1121-
msg = txt;
1121+
msg = txt;
11221122
}
11231123
}
11241124
else
11251125
msg = rule.alertTextOk;
11261126

11271127
// see if we should display a green prompt
11281128
if (msg)
1129-
methods._showPrompt(errorField, msg, "pass", true, options);
1129+
methods._showPrompt(errorField, msg, "pass", true, options);
11301130
else
1131-
methods._closePrompt(errorField);
1131+
methods._closePrompt(errorField);
11321132
}
11331133
}
11341134
errorField.trigger("jqv.field.result", [errorField, options.isError, msg]);
@@ -1183,9 +1183,9 @@
11831183
// Because no error was found befor submitting
11841184
if(ajaxform) prompt = false;
11851185
if (prompt)
1186-
methods._updatePrompt(field, prompt, promptText, type, ajaxed, options);
1186+
methods._updatePrompt(field, prompt, promptText, type, ajaxed, options);
11871187
else
1188-
methods._buildPrompt(field, promptText, type, ajaxed, options);
1188+
methods._buildPrompt(field, promptText, type, ajaxed, options);
11891189
},
11901190
/**
11911191
* Builds and shades a prompt for the given field.
@@ -1232,7 +1232,7 @@
12321232
if (typeof(positionType)=='string')
12331233
{
12341234
var pos=positionType.indexOf(":");
1235-
if(pos!=-1)
1235+
if(pos!=-1)
12361236
positionType=positionType.substring(0,pos);
12371237
}
12381238

@@ -1346,9 +1346,9 @@
13461346
* @return undefined or the error prompt (jqObject)
13471347
*/
13481348
_getPrompt: function(field) {
1349-
var formId = $(field).closest('form').attr('id');
1349+
var formId = $(field).closest('form').attr('id');
13501350
var className = methods._getClassName(field.attr("id")) + "formError";
1351-
var match = $("." + methods._escapeExpression(className) + '.parentForm' + formId)[0];
1351+
var match = $("." + methods._escapeExpression(className) + '.parentForm' + formId)[0];
13521352
if (match)
13531353
return $(match);
13541354
},

js/languages/jquery.validationEngine-cz.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
"alertText": "* Tato položka je povinná",
1010
"alertTextCheckboxMultiple": "* Prosím vyberte jednu možnost",
1111
"alertTextCheckboxe": "* Tato položka je povinná"
12+
},
13+
"requiredInFunction": {
14+
"func": function(field, rules, i, options){
15+
return (field.val() == "test") ? true : false;
16+
},
17+
"alertText": "* Field must equal test"
1218
},
1319
"minSize": {
1420
"regex": "none",

js/languages/jquery.validationEngine-da.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"alertTextCheckboxMultiple": "* Vælg venligst en af mulighederne",
1111
"alertTextCheckboxe": "* Dette felt er krævet"
1212
},
13+
"requiredInFunction": {
14+
"func": function(field, rules, i, options){
15+
return (field.val() == "test") ? true : false;
16+
},
17+
"alertText": "* Field must equal test"
18+
},
1319
"minSize": {
1420
"regex": "none",
1521
"alertText": "* Minimum ",

js/languages/jquery.validationEngine-de.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"alertTextCheckboxMultiple": "* Bitte wählen Sie eine Option",
1212
"alertTextCheckboxe": "* Dieses Feld ist ein Pflichtfeld"
1313
},
14+
"requiredInFunction": {
15+
"func": function(field, rules, i, options){
16+
return (field.val() == "test") ? true : false;
17+
},
18+
"alertText": "* Field must equal test"
19+
},
1420
"minSize": {
1521
"regex": "none",
1622
"alertText": "* Mindestens ",

js/languages/jquery.validationEngine-en.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"alertTextCheckboxe": "* This checkbox is required",
1212
"alertTextDateRange": "* Both date range fields are required"
1313
},
14+
"requiredInFunction": {
15+
"func": function(field, rules, i, options){
16+
return (field.val() == "test") ? true : false;
17+
},
18+
"alertText": "* Field must equal test"
19+
},
1420
"dateRange": {
1521
"regex": "none",
1622
"alertText": "* Invalid ",

js/languages/jquery.validationEngine-es.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"alertTextCheckboxMultiple": "* Por favor seleccione una opción",
1212
"alertTextCheckboxe": "* Este checkbox es obligatorio"
1313
},
14+
"requiredInFunction": {
15+
"func": function(field, rules, i, options){
16+
return (field.val() == "test") ? true : false;
17+
},
18+
"alertText": "* Field must equal test"
19+
},
1420
"minSize": {
1521
"regex": "none",
1622
"alertText": "* Mínimo de ",

js/languages/jquery.validationEngine-et.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"alertTextCheckboxe": "* Linnuke on nõutud",
1212
"alertTextDateRange": "* Mõlemad kuupäeva valikud on nõutud"
1313
},
14+
"requiredInFunction": {
15+
"func": function(field, rules, i, options){
16+
return (field.val() == "test") ? true : false;
17+
},
18+
"alertText": "* Field must equal test"
19+
},
1420
"dateRange": {
1521
"regex": "none",
1622
"alertText": "* Vigane ",

js/languages/jquery.validationEngine-fa.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"alertTextCheckboxe": "* این چک باکس ضروری است",
1212
"alertTextDateRange": "* هر دو فیلد‌های بازه‌ی تاریخی ضروری هستند"
1313
},
14+
"requiredInFunction": {
15+
"func": function(field, rules, i, options){
16+
return (field.val() == "test") ? true : false;
17+
},
18+
"alertText": "* Field must equal test"
19+
},
1420
"dateRange": {
1521
"regex": "none",
1622
"alertText": "* بازه‌ی تاریخی ",

js/languages/jquery.validationEngine-fi.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"alertTextCheckboxMultiple": "* Yksi valikoima, kiitos",
1111
"alertTextCheckboxe": "* Tarkistusmerkki on pakollinen"
1212
},
13+
"requiredInFunction": {
14+
"func": function(field, rules, i, options){
15+
return (field.val() == "test") ? true : false;
16+
},
17+
"alertText": "* Field must equal test"
18+
},
1319
"minSize": {
1420
"regex": "none",
1521
"alertText": "* Vähintään ",

js/languages/jquery.validationEngine-fr.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"alertTextCheckboxMultiple": "* Choisir une option",
1111
"alertTextCheckboxe": "* Cette option est requise"
1212
},
13+
"requiredInFunction": {
14+
"func": function(field, rules, i, options){
15+
return (field.val() == "test") ? true : false;
16+
},
17+
"alertText": "* Field must equal test"
18+
},
1319
"minSize": {
1420
"regex": "none",
1521
"alertText": "* Minimum ",

js/languages/jquery.validationEngine-hr.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"alertTextCheckboxe": "* Ovaj odabir je obavezan",
1212
"alertTextDateRange": "* Oba datuma su obavezna"
1313
},
14+
"requiredInFunction": {
15+
"func": function(field, rules, i, options){
16+
return (field.val() == "test") ? true : false;
17+
},
18+
"alertText": "* Field must equal test"
19+
},
1420
"dateRange": {
1521
"regex": "none",
1622
"alertText": "* Greška ",

js/languages/jquery.validationEngine-hu.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"alertTextCheckboxe": "* Ez az opció be kell legyen jelölve",
1212
"alertTextDateRange": "* Mindkét dátum mezőt ki kell tölteni"
1313
},
14+
"requiredInFunction": {
15+
"func": function(field, rules, i, options){
16+
return (field.val() == "test") ? true : false;
17+
},
18+
"alertText": "* Field must equal test"
19+
},
1420
"dateRange": {
1521
"regex": "none",
1622
"alertText": "* Érvénytelen ",

js/languages/jquery.validationEngine-it.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
"alertTextCheckboxMultiple": "* Per favore selezionare un'opzione",
1010
"alertTextCheckboxe": "* E' richiesta la selezione della casella"
1111
},
12+
"requiredInFunction": {
13+
"func": function(field, rules, i, options){
14+
return (field.val() == "test") ? true : false;
15+
},
16+
"alertText": "* Field must equal test"
17+
},
1218
"length": {
1319
"regex": "none",
1420
"alertText": "* Fra ",

js/languages/jquery.validationEngine-ja.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"alertTextCheckboxMultiple": "* 選択してください",
1818
"alertTextCheckboxe": "* チェックボックスをチェックしてください"
1919
},
20+
"requiredInFunction": {
21+
"func": function(field, rules, i, options){
22+
return (field.val() == "test") ? true : false;
23+
},
24+
"alertText": "* Field must equal test"
25+
},
2026
"minSize": {
2127
"regex": "none",
2228
"alertText": "* ",

js/languages/jquery.validationEngine-nl.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"alertTextCheckboxMultiple": "* Selecteer a.u.b. een optie",
1111
"alertTextCheckboxe": "* Dit selectievakje is verplicht"
1212
},
13+
"requiredInFunction": {
14+
"func": function(field, rules, i, options){
15+
return (field.val() == "test") ? true : false;
16+
},
17+
"alertText": "* Field must equal test"
18+
},
1319
"minSize": {
1420
"regex": "none",
1521
"alertText": "* Minimaal ",

js/languages/jquery.validationEngine-pl.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"alertTextCheckboxMultiple": "* Proszę wybrać opcję",
1111
"alertTextCheckboxe": "* Pole wymagane"
1212
},
13+
"requiredInFunction": {
14+
"func": function(field, rules, i, options){
15+
return (field.val() == "test") ? true : false;
16+
},
17+
"alertText": "* Field must equal test"
18+
},
1319
"minSize": {
1420
"regex": "none",
1521
"alertText": "* Minimalna liczba znaków to ",

js/languages/jquery.validationEngine-pt.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"alertTextCheckboxMultiple": "* Selecione uma opção",
1111
"alertTextCheckboxe": "* Campo obrigatório"
1212
},
13+
"requiredInFunction": {
14+
"func": function(field, rules, i, options){
15+
return (field.val() == "test") ? true : false;
16+
},
17+
"alertText": "* Field must equal test"
18+
},
1319
"minSize": {
1420
"regex": "none",
1521
"alertText": "* Mínimo ",

0 commit comments

Comments
 (0)