|
/* |
|
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** |
|
** |
|
** Le Amateur jQuery Form Validator Plugin |
|
** build 1.3.0 (23, Apr 2017) |
|
** © Pedro Fernandes |
|
** |
|
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** |
|
*/ |
|
|
|
$.fn.extend({ |
|
validate: function(data, callback) { |
|
'use strict'; |
|
|
|
// REMOVE WHITE SPACE |
|
String.prototype.remWS = function() { |
|
return this.replace(/ /g, '').trim(); |
|
}; |
|
|
|
// SINGLE WHITE SPACE |
|
String.prototype.sinWS = function() { |
|
return this.replace(/[ ]+/g, ' ').trim(); |
|
}; |
|
|
|
// REMOVE SIGNAL |
|
String.prototype.remSN = function() { |
|
return this.replace(/[\+]/g, ''); |
|
}; |
|
|
|
var alerts = { |
|
required: function(display) { |
|
return 'The <strong>' + display + '</strong> field is required.'; |
|
}, |
|
length: function(display, min, max, given) { |
|
return 'The <strong>' + display + '</strong> field must contain between ' + min + ' and ' + max + ' characters. ' + given + ' given.'; |
|
}, |
|
exact_len: function(display, len, given) { |
|
return 'The <strong>' + display + '</strong> field must contain exactly ' + len + ' characters. ' + given + ' given.'; |
|
}, |
|
alpha: function(display) { |
|
return 'The <strong>' + display + '</strong> field can contain alpha characters only.'; |
|
}, |
|
int: function(display) { |
|
return 'The <strong>' + display + '</strong> field must contain an integer number.'; |
|
}, |
|
real: function(display) { |
|
return 'The <strong>' + display + '</strong> field must contain a real number.'; |
|
}, |
|
pos: function(display) { |
|
return 'The <strong>' + display + '</strong> field must contain a positive number.'; |
|
}, |
|
alphanum: function(display) { |
|
return 'The <strong>' + display + '</strong> field can contain alpha-numeric characters only.'; |
|
}, |
|
email: function(display) { |
|
return 'The <strong>' + display + '</strong> field must contain an valid email address.'; |
|
}, |
|
match: function(display) { |
|
return 'The <strong>' + display + '</strong> field doesn\'t match the required field.'; |
|
}, |
|
special: function(display) { |
|
return 'The <strong>' + display + '</strong> field can contain alpha-numeric & common special characters only.'; |
|
}, |
|
range: function(display, min, max, given) { |
|
return 'The <strong>' + display + '</strong> field must contain a number between ' + min + ' an ' + max + '. ' + given + ' given.'; |
|
}, |
|
nospace: function(display) { |
|
return 'The <strong>' + display + '</strong> field can not contain white-spaces or linebreaks.'; |
|
} |
|
}; |
|
|
|
var fields = []; |
|
var displays = []; |
|
var rules = []; |
|
|
|
for (var x in this[0].elements) { |
|
|
|
for (var i in data) { |
|
if (data[i].name === this[0].elements[x].name && fields.indexOf(this[0].elements[x]) === -1) { |
|
fields.push(this[0].elements[x]); |
|
displays.push(data[i].display); |
|
rules.push(data[i].rules.split('|')); |
|
} |
|
} |
|
|
|
} |
|
|
|
// PERFORM THE VALIDATOR |
|
for (var i in fields) { |
|
|
|
//----------------------- |
|
// REQUIRED (required) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'required' && fields[i].value.remWS() === '') { |
|
$(fields[i]).val(fields[i].value.remWS()); |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.required(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.required(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
//----------------------- |
|
// ALPHA (alpha) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'alpha' && /[^\n a-zãõñâêîôûáéíóúàèìòùäëïöüç]/i.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.alpha(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.alpha(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// ALPHA-NUM (alpha_num) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'alpha_num' && /[^\n a-z0-9ãõñâêîôûáéíóúàèìòùäëïöüç]/i.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.alphanum(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.alphanum(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// SPECIAL (special) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'special' && /[^\n 0-9a-zãõñâêîôûáéíóúàèìòùäëïöüç_\-!@#$%&*]+/i.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.special(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.special(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// NO-SPACES (nospace) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'nospace' && /[ \n]+/i.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.nospace(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.nospace(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// INTEGER (int) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'int' && /[^\-+0-9]/i.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.int(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.int(displays[i])}); |
|
return false; |
|
} else { |
|
fields[i].value = fields[i].value.remSN(); |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// REAL (real) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'real' && !/^([\-+0-9]+)([.0-9])([0-9]+)$/.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.real(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.real(displays[i])}); |
|
return false; |
|
} else { |
|
fields[i].value = fields[i].value.remSN(); |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// POSITIVE (pos) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'pos' && !/^([+0-9.]+)$/i.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.pos(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.pos(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// EMAIL (email) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (rules[i][x] === 'email' && !/^([\w\-\.]+)@([\a-z0-9\.-]+)([a-z0-9]{2,3})$/i.test(fields[i].value)) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.email(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.email(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// MATCH (match[name]) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
|
|
if (/match/.test(rules[i][x])) { |
|
|
|
var _match = rules[i][x].match(/(?:\[)([a-z0-9\-_]+)/i)[1].toString(); |
|
var _required = document.getElementsByName(_match)[0].value; |
|
|
|
if (fields[i].value !== _required) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.match(displays[i])); |
|
if (callback) callback({success: 0, msg: alerts.match(displays[i])}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// LENGTH (len[min,max] or len[exactly]) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
if (/len/.test(rules[i][x])) { |
|
|
|
var _range = rules[i][x].match(/(\d+)/g); |
|
var _given = fields[i].value.sinWS().length; |
|
|
|
if (_range[1] === undefined && _given != _range[0]) { |
|
$(fields[i]).focus(); |
|
fields[i].value = fields[i].value.sinWS(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.exact_len(displays[i], _range[0], _given)); |
|
if (callback) callback({success: 0, msg: alerts.exact_len(displays[i], _range[0], _given)}); |
|
return false; |
|
} else { |
|
fields[i].value = fields[i].value.sinWS(); |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
} |
|
|
|
if (_given < _range[0] || _given > _range[1]) { |
|
$(fields[i]).focus(); |
|
fields[i].value = fields[i].value.sinWS(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.length(displays[i], _range[0], _range[1], _given)); |
|
if (callback) callback({success: 0, msg: alerts.length(displays[i], _range[0], _range[1], _given)}); |
|
return false; |
|
} else { |
|
fields[i].value = fields[i].value.sinWS(); |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
} |
|
|
|
|
|
|
|
//----------------------- |
|
// RANGE (range[min,max]) |
|
//----------------------- |
|
for (var x in rules[i]) { |
|
if (/range/.test(rules[i][x])) { |
|
|
|
var _range = rules[i][x].match(/(\d+)/g); |
|
var _given = parseFloat(fields[i].value); |
|
|
|
if (_given < parseFloat(_range[0]) || _given > parseFloat(_range[1])) { |
|
$(fields[i]).focus(); |
|
$(fields[i]).parent().addClass('has-error'); |
|
$(fields[i]).next().html(alerts.range(displays[i], _range[0], _range[1], _given)); |
|
if (callback) callback({success: 0, msg: alerts.range(displays[i], _range[0], _range[1], _given)}); |
|
return false; |
|
} else { |
|
$(fields[i]).parent().removeClass('has-error'); |
|
$(fields[i]).next().html(''); |
|
}; |
|
|
|
} |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (callback) callback({ |
|
success: 1, |
|
msg: 'submitted successfully!' |
|
}); |
|
} |
|
}); |