Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for stronger regular expressions #558

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions additional-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ jQuery.validator.addMethod("ipv6", function(value, element, param) {
}, "Please enter a valid IP v6 address.");

/**
* Return true if the field value matches the given format RegExp
* Return true if the field value matches the given format RegExp.
* Supports 3 regular expression modifiers: i, m, and g.
*
* @example jQuery.validator.methods.pattern("AR1004",element,/^AR\d{4}$/)
* @result true
Expand All @@ -325,15 +326,19 @@ jQuery.validator.addMethod("ipv6", function(value, element, param) {
* @cat Plugins/Validate/Methods
*/
jQuery.validator.addMethod("pattern", function(value, element, param) {
if (this.optional(element)) {
return true;
}
if (typeof param === 'string') {
param = new RegExp('^(?:' + param + ')$');
parts = param.split("/");
modifiers = parts[parts.length - 1];
if (!/^[img]+$/.test(modifiers)) {
modifiers = '';
}
param = param.replace(/(.*\/)[img]+$/, "$1");
param = param.replace(/^\//, '');
param = param.replace(/\/$/, '');
param = new RegExp(param, modifiers);
}
return param.test(value);
}, "Invalid format.");

return this.optional(element) || param.test(value);
},

/*
* Lets you say "at least X inputs that match selector Y must be filled."
Expand Down