Skip to content

Commit

Permalink
Allow passing of a parameter to validation rule and added min and max…
Browse files Browse the repository at this point in the history
… length rules
  • Loading branch information
lopsided committed Mar 18, 2015
1 parent d1e866d commit dffdc32
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
16 changes: 15 additions & 1 deletion dist/angular-validation-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
},
url: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,
email: /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
number: /^\d+$/
number: /^\d+$/,
minlength: function(value, scope, element, attrs, param) {
return value.length >= param;
},
maxlength: function(value, scope, element, attrs, param) {
return value.length <= param;
}
};

var defaultMsg = {
Expand All @@ -28,6 +34,14 @@
number: {
error: 'This should be Number',
success: 'It\'s Number'
},
minlength: {
error: 'This should be longer',
success: 'Long enough!'
},
maxlength: {
error: 'This should be shorter',
success: 'Short enough!'
}
};

Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validation-rule.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@
var checkValidation = function(scope, element, attrs, ctrl, validation, value) {

var validators = validation.slice(0),
validator = validators[0].trim(),
validatorExpr = validators[0].trim(),
paramIndex = validatorExpr.indexOf('='),
validator = paramIndex === -1 ? validatorExpr : validatorExpr.substr(0, paramIndex),
validatorParam = paramIndex === -1 ? null : validatorExpr.substr(paramIndex + 1),
leftValidation = validators.slice(1),
successMessage = validator + 'SuccessMessage',
errorMessage = validator + 'ErrorMessage',
Expand Down Expand Up @@ -413,7 +416,7 @@
}
// Check with Function
if (expression.constructor === Function) {
return $q.all([$validationProvider.getExpression(validator)(value, scope, element, attrs)])
return $q.all([$validationProvider.getExpression(validator)(value, scope, element, attrs, validatorParam)])
.then(function(data) {
if (data && data.length > 0 && data[0]) {
return valid.success();
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@
var checkValidation = function(scope, element, attrs, ctrl, validation, value) {

var validators = validation.slice(0),
validator = validators[0].trim(),
validatorExpr = validators[0].trim(),
paramIndex = validatorExpr.indexOf('='),
validator = paramIndex === -1 ? validatorExpr : validatorExpr.substr(0, paramIndex),
validatorParam = paramIndex === -1 ? null : validatorExpr.substr(paramIndex + 1),
leftValidation = validators.slice(1),
successMessage = validator + 'SuccessMessage',
errorMessage = validator + 'ErrorMessage',
Expand Down Expand Up @@ -121,7 +124,7 @@
}
// Check with Function
if (expression.constructor === Function) {
return $q.all([$validationProvider.getExpression(validator)(value, scope, element, attrs)])
return $q.all([$validationProvider.getExpression(validator)(value, scope, element, attrs, validatorParam)])
.then(function(data) {
if (data && data.length > 0 && data[0]) {
return valid.success();
Expand Down
16 changes: 15 additions & 1 deletion src/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
},
url: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,
email: /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
number: /^\d+$/
number: /^\d+$/,
minlength: function(value, scope, element, attrs, param) {
return value.length >= param;
},
maxlength: function(value, scope, element, attrs, param) {
return value.length <= param;
}
};

var defaultMsg = {
Expand All @@ -28,6 +34,14 @@
number: {
error: 'This should be Number',
success: 'It\'s Number'
},
minlength: {
error: 'This should be longer',
success: 'Long enough!'
},
maxlength: {
error: 'This should be shorter',
success: 'Short enough!'
}
};

Expand Down

0 comments on commit dffdc32

Please sign in to comment.