Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validation rules for the Angular Validation library - https://github.com/huei90/angular-validation/ .
/* jshint regexdash: false */
(function() {
angular.module('validation.rule', ['validation'])
.config(['$validationProvider', function($validationProvider) {
var expression = {
required: function(value) {
return !!value;
},
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+$/,
username: /^[a-zA-Z]{4,}$/,
name: /^[A-Za-z ]+$/,
phone: /^\((\d{3})\) (\d{3})[- ](\d{4})$/,
pin: /^\d{4}$/,
zip: /^\d{5}$/,
cvv: /^[0-9]{3,4}$/
};
var defaultMsg = {
required: {
error: 'This should be Required!!',
success: 'It\'s Required'
},
url: {
error: 'Please enter a valid URL',
success: 'Perfect!'
},
email: {
error: 'A valid email address is required.',
success: 'Perfect!'
},
number: {
error: 'This must be numbers only.',
success: 'Perfect!'
},
username: {
error: 'Your username must consist of only letters and be at least 4 characters long.',
success: 'Perfect!'
},
name: {
error: 'Your name must contain only letters and spaces.',
success: 'Perfect!'
},
phone: {
error: 'Please enter a valid US cell phone number.',
success: 'Perfect!'
},
pin: {
error: 'Please choose a 4 digit pin number.',
success: 'Perfect!'
},
zip: {
error: 'Please enter your 5 digit zip code.',
success: 'Perfect!'
},
cvv: {
error: 'Please enter your 3 or 4 digit CVV number.',
success: 'Perfect!'
}
};
$validationProvider.setExpression(expression).setDefaultMsg(defaultMsg);
}]);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment