// This is an IE 8 hack - probably should be global
if (!Object.keys) {
Object.keys = function(obj) {
var keys = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
keys.push(i);
}
}
return keys;
};
}
/* jQuery plugin for inputs, selects, textareas
so that error(), hasRule() and isRequired() can
be member functions of elements rather than having
to pass the element in to these functions as a parameter.
IMHO this is more logical. Note that function names are
passed into the $.validate function to avoid cluttering
the jQuery namespace. See:
https://learn.jquery.com/plugins/basic-plugin-creation/
This is the same design pattern as used by $.dialog and others.
*/
(function ($) {
$.fn.validate = function( method, arg ) {
var self = this;
function error(errorText) {
var $label = self.siblings('label');
if(!$label.find(formCheck.validationErrorSelector).length) {
$label.append(' ' + errorText + '');
}
self.addClass(formCheck.validationErrorClass);
}
function hasRule(rule) {
var toReturn = false;
if(self[0].hasAttribute('data-validation')) {
var rules = self.attr('data-validation').split('|');
$.each(rules, function(index, value) {
if(value == rule) {
toReturn = true;
return false;
}
});
}
return toReturn;
}
function isRequired() {
// check for "required" attribute
if(self[0].hasAttribute('required')) {
return true;
}
// check for required rule in data-validation attr
if(hasRule('required')) return true;
// check for req_ prefix on field name
if(self[0].hasAttribute('name') && (self.attr('name').substr(0, 4) == 'req_') ) return true;
return false;
}
if( method === 'error' ) {
error(arg);
}
if( method === 'hasRule' ) {
return hasRule(arg);
}
if( method === 'isRequired' ) {
return isRequired();
}
};
}( jQuery ));
var formCheck = {
validationErrorClass: 'da-validation-error',
validationErrorSelector: '.da-validation-error',
totalChecks: 0,
checksCompleted: 0,
form: null,
initFormCheck: function (scope){
scope = scope || document;
formCheck.antiSpamForms(scope);
formCheck.initValidation(scope);
},
antiSpamForms: function (scope) {
scope = scope || document;
$(scope).find('form').each(function(){
formCheck.antiSpamFields($(this));
});
},
antiSpamFields: function(form){
$(form).find('input[name="daformref"]').attr('value' , '');
if($(form).find('input[name="daisnotspam"]').length < 1){
var antispaminput = '';
$(antispaminput).prependTo(form);
}
},
initValidation: function(scope) {
scope = scope || document;
// init normal, non-ajaxian forms
$(scope).find('form.da-validate').each(function() {
// fix jquery bug where form elements named "submit" cause triggering submit event on forms to fail
$(this).find('[name=submit]').attr('name', '');
$(this).submit(function(e) {
if(!$(this).data('passed-validation')) {
e.preventDefault();
var $form = $(this);
formCheck.checkForm($(this), function() {
$form.data('passed-validation', true);
$form.submit();
});
}
});
});
} ,
callback: function() {} ,
doneCheck: function() {
formCheck.checksCompleted++;
if(formCheck.checksCompleted == formCheck.totalChecks) {
// finished validation!
// check for errors
if(!formCheck.form.find(formCheck.validationErrorSelector).length) {
// no errors - run callback
formCheck.callback();
}
}
} ,
checkForm: function($form, callback) {
formCheck.form = $form;
// reset counters
formCheck.totalChecks = 0;
formCheck.checksCompleted = 0;
if(typeof callback == 'function') {
formCheck.callback = callback;
}
// clear errors
$form.find('span' + formCheck.validationErrorSelector).remove();
$form.find(formCheck.validationErrorSelector).removeClass(formCheck.validationErrorClass);
// count total checks to be done
$form.find('input, select, textarea').each(function() {
formCheck.totalChecks += Object.keys(formCheck.checks).length;
});
// iterate over form fields
$form.find('input, select, textarea').each(function() {
var $formfield = $(this);
// run checks
$.each(formCheck.checks, function(k, v) {
v($formfield);
});
});
} ,
// DEFINE CHECKS
checks: {
isNotEmpty: function($formfield) {
if($formfield.validate('isRequired') && $formfield.val() == '') {
$formfield.validate('error', 'Required');
}
formCheck.doneCheck();
} ,
isValidEmail: function($formfield) {
if($formfield.attr('type') == 'email' || $formfield.attr('name') == 'email' || $formfield.attr('name') == 'req_email') {
var required = false;
if($formfield.attr('name') == 'req_email' || $formfield.prop('required')){
required = true;
}
if(!required && !$formfield.val()){
formCheck.doneCheck();
return true;
}
// http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(!re.test($formfield.val())) {
$formfield.validate('error', 'Invalid email');
}
}
formCheck.doneCheck();
},
isFullName: function($formfield) {
if($formfield.validate('hasRule', 'fullname')) {
if($formfield.val().split(' ').length < 2) {
$formfield.validate('error', 'Full name required');
}
}
formCheck.doneCheck();
}
} ,
addCheck: function(name, method) {
formCheck.checks[name] = method;
}
};
$(document).ready(function(){
formCheck.initFormCheck();
});