-
Notifications
You must be signed in to change notification settings - Fork 64
/
customValidations.js
34 lines (26 loc) · 1.12 KB
/
customValidations.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
require('jquery');
module.exports = function () {
//TODO this nino validator can removed and placed in the html input as attribute pattern="^[A-Za-z]{2}\d{6}[A-Za-z]$"
jQuery.validator.addMethod('nino', function (value, element) {
return /^[A-Za-z]{2}\d{6}[A-Za-z]$/.test(value);
});
// Check if value of input is correctly contained in suggestion data
jQuery.validator.addMethod('suggestion', function (value, element) {
var suggestions = window[element.getAttribute('data-suggestions')];
var validSuggestion = false;
$(suggestions).each(function (index, suggestion) {
if (value.toLowerCase() === suggestion.title.toLowerCase() || value === suggestion.value) {
validSuggestion = true;
return false;
}
});
return validSuggestion;
});
// Use the pattern attribute on your input with a valid regex
jQuery.validator.addMethod('pattern', function (value, element, pattern) {
var dataAttributeFlag = element.getAttribute('data-pattern-flags');
var flag = dataAttributeFlag || '';
var regex = new RegExp(pattern, flag);
return regex.test(value);
});
};