Skip to content

Instantly share code, notes, and snippets.

@anagri
Created June 20, 2011 10:45
Show Gist options
  • Save anagri/1035425 to your computer and use it in GitHub Desktop.
Save anagri/1035425 to your computer and use it in GitHub Desktop.
validation framework
;
var validator = $('form').expValidate({
rules: {
cityname: 'required',
checkinDate: ['required', 'date'],
checkoutDate: ['required', 'date']
},
groups: {
dateRange: ['checkinDate', 'checkoutDate']
},
methods: {
dateRange: function(form) {
},
validChildAge: function(form) {
}
},
messages: {
cityname: {
required: 'Please input a destination'
},
date: 'Date format is invalid',
checkinDate: {
date: 'Please enter a checkin date'
}
},
config: {
ignoreDuplicateMessages: true
}
});
validator.addRules('room1_child1', function() {}, 'Please specify age for children below');
validator.addRules('room1_child1', 'validChildAge', 'Please specify age for children below');
(function($) {
$.extend($.fn, {
expValidate: function(options) {
var validator = $.data(this[0], 'validator');
if(validator) return validator;
validator = new $.validator(this[0], options);
$.data(this[0], 'validator', validator);
this[0].submit(function() {
return validator.validate();
});
return this;
}
});
$.validator = function(form, options) {
this.settings = $.extend(true, {}, this.defaultOptions, options);
this.currentForm = form;
this.init();
};
$.extend($.validator, {
prototype: {
init: function() {
},
validate: function() {
function validateRules() {
var rules = this.settings.rules;
for(var rule in rules) {
var elements = rules[rule];
var method = this.settings.methods[rule];
if(typeof method == 'undefined') throw "Method not defined for "+ rule;
method()
}
}
function highlightErrors() {
}
var result = validateRules();
if(!result) highlightErrors();
return result;
}
},
defaultOptions: {
messages: {
required: 'Please enter a value for given field.'
},
methods: {
required: function(form, element) {
return element.value != '';
}
}
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment