vpn.email
Version:
vpn.email client
424 lines (372 loc) • 13.8 kB
JavaScript
$.widget( "metro.validator" , {
version: "1.0.0",
options: {
showErrorState: true,
showErrorHint: true,
showRequiredState: true,
showSuccessState: true,
hintSize: 0,
hintBackground: '#FFFCC0',
hintColor: '#000000',
hideError: 2000,
hideHint: 5000,
hintEasing: 'easeInQuad',
hintEasingTime: 400,
hintMode: 'hint', // hint, line
hintPosition: 'right',
focusInput: true,
onBeforeSubmit: function(form, result){return true;},
onErrorInput: function(input){},
onSubmit: function(form){return true;}
},
_scroll: 0,
funcs: {
required: function(val){
return val.trim() !== "";
},
minlength: function(val, len){
if (len == undefined || isNaN(len) || len <= 0) {
return false;
}
return val.trim().length >= len;
},
maxlength: function(val, len){
if (len == undefined || isNaN(len) || len <= 0) {
return false;
}
return val.trim().length <= len;
},
min: function(val, min_value){
if (min_value == undefined || isNaN(min_value)) {
return false;
}
if (val.trim() === "") {
return false;
}
if (isNaN(val)) {
return false;
}
return val >= min_value;
},
max: function(val, max_value){
if (max_value == undefined || isNaN(max_value)) {
return false;
}
if (val.trim() === "") {
return false;
}
if (isNaN(val)) {
return false;
}
return val <= max_value;
},
email: function(val){
return /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test(val);
},
url: function(val){
return /^(?:[a-z]+:)?\/\//i.test(val);
},
date: function(val){
return !!(new Date(val) !== "Invalid Date" && !isNaN(new Date(val)));
},
number: function(val){
return (val - 0) == val && (''+val).trim().length > 0;
},
digits: function(val){
return /^\d+$/.test(val);
},
hexcolor: function(val){
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(val);
},
pattern: function(val, pat){
if (pat == undefined) {
return false;
}
var reg = new RegExp(pat);
return reg.test(val);
}
},
_create: function () {
var that = this, element = this.element, o = this.options;
$.each(element.data(), function(key, value){
if (key in o) {
try {
o[key] = $.parseJSON(value);
} catch (e) {
o[key] = value;
}
}
});
if (o.hintMode !== 'line') {
o.hintMode = 'hint2';
}
this._scroll = $(window).scrollTop();
this._createValidator();
element.data('validator', this);
},
_createValidator: function(){
var that = this, element = this.element, o = this.options;
var inputs = element.find("[data-validate-func]");
element.attr('novalidate', 'novalidate');
if (o.showRequiredState) {
$.each(inputs, function(){
var input = $(this);
if (input.parent().hasClass('input-control')) {
input.parent().addClass('required');
} else {
input.addClass('required');
}
});
}
inputs.on('focus', function(){
});
//console.log(this._scroll);
$(window).scroll(function(e){
var st = $(this).scrollTop();
var delta = isNaN(st - this._scroll) ? 0 : st - this._scroll;
$(".validator-hint.hint2").css({
top: '-='+delta
});
this._scroll = st;
});
if (element[0].onsubmit) {
this._onsubmit = element[0].onsubmit;
element[0].onsubmit = null;
} else {
this._onsubmit = null;
}
element[0].onsubmit = function(){
return that._submit();
};
},
_submit: function(){
var that = this, element = this.element, o = this.options;
var inputs = element.find("[data-validate-func]");
var submit = element.find(":submit").attr('disabled', 'disabled').addClass('disabled');
var result = 0;
$('.validator-hint').hide();
inputs.removeClass('error success');
$.each(inputs, function(i, v){
var input = $(v);
if (input.parent().hasClass('input-control')) {
input.parent().removeClass('error success');
}
});
$.each(inputs, function(i, v){
var this_result = true;
var input = $(v);
var func = input.data('validateFunc') != undefined ? String(input.data('validateFunc')).split(",") : [],
arg = input.data('validateArg') != undefined ? String(input.data('validateArg')).split(",") : [];
console.log(input.data('validateArg'));
$.each(func, function(i, func_name){
if (!this_result) return;
var _args = arg[i] != undefined ? arg[i] : false;
this_result = that.funcs[func_name.trim()](input.val(), _args);
});
// this_result = that.funcs[func](input.val(), arg);
if (!this_result) {
if (typeof o.onErrorInput === 'function') {
o.onErrorInput(input);
} else {
if (typeof window[o.onErrorInput] === 'function') {
window[o.onErrorInput](input);
} else {
result = eval("(function(){"+o.onErrorInput+"})");
result.call(input);
}
}
}
if (!this_result && o.showErrorState) {
that._showError(input);
}
if (!this_result && o.showErrorHint) {
setTimeout(function(){
that._showErrorHint(input);
}, i*100);
}
if (this_result && o.showSuccessState) {
that._showSuccess(input);
}
if (!this_result && i == 0 && o.focusInput) {
input.focus();
}
result += !this_result ? 1 : 0;
});
if (typeof o.onBeforeSubmit === 'function') {
result += !o.onBeforeSubmit(element, result) ? 1 : 0;
} else {
if (typeof window[o.onBeforeSubmit] === 'function') {
result += window[o.onBeforeSubmit](element, result) ? 1 : 0;
} else {
var f0 = eval("(function(){"+o.onBeforeSubmit+"})");
result += f0.call(element, result) ? 1 : 0;
}
}
if (result !== 0) {
submit.removeAttr('disabled').removeClass('disabled');
return false;
}
if (typeof o.onSubmit === 'function') {
result = o.onSubmit(element[0]);
} else {
if (typeof window[o.onSubmit] === 'function') {
result = window[o.onSubmit](element[0]);
} else {
var f = eval("(function(){"+o.onSubmit+"})");
result = f.call(element[0]);
}
}
submit.removeAttr('disabled').removeClass('disabled');
return result;
},
_showSuccess: function(input){
if (input.parent().hasClass('input-control')) {
input.parent().addClass('success');
} else {
input.addClass('success');
}
},
_showError: function(input){
var o = this.options;
if (input.parent().hasClass('input-control')) {
input.parent().addClass('error');
} else {
input.addClass('error');
}
if (o.hideError && o.hideError > 0) {
setTimeout(function(){
input.parent().removeClass('error');
}, o.hideError);
}
},
_showErrorHint: function(input){
var o = this.options,
msg = input.data('validateHint'),
pos = input.data('validateHintPosition') || o.hintPosition,
mode = input.data('validateHintMode') || o.hintMode,
background = input.data('validateHintBackground') || o.hintBackground,
color = input.data('validateHintColor') || o.hintColor;
var hint, top, left;
if (msg === undefined) {
return false;
}
hint = $("<div/>").addClass(mode+' validator-hint');//.appendTo(input.parent());
hint.html(msg !== undefined ? this._format(msg, input.val()) : '');
hint.css({
'min-width': o.hintSize
});
if (background.isColor()) {
hint.css('background-color', background);
} else {
hint.addClass(background);
}
if (color.isColor()) {
hint.css('color', color);
} else {
hint.addClass(color);
}
// Position
if (mode === 'line') {
hint.addClass('hint2').addClass('line');
hint.css({
'position': 'relative',
'width': input.parent().hasClass('input-control') ? input.parent().width() : input.width(),
'z-index': 100
});
hint.appendTo(input.parent());
hint.fadeIn(o.hintEasingTime, function(){
setTimeout(function () {
hint.hide().remove();
}, o.hideHint);
});
} else {
hint.appendTo("body");
// right
if (pos === 'right') {
left = input.offset().left + input.outerWidth() + 15 - $(window).scrollLeft();
top = input.offset().top + input.outerHeight() / 2 - hint.outerHeight() / 2 - $(window).scrollTop() - 10;
hint.addClass(pos);
hint.css({
top: top,
left: $(window).width() + 100
});
hint.show().animate({
left: left
}, o.hintEasingTime, o.hintEasing, function () {
setTimeout(function () {
hint.hide().remove();
}, o.hideHint);
});
} else if (pos === 'left') {
left = input.offset().left - hint.outerWidth() - 10 - $(window).scrollLeft();
top = input.offset().top + input.outerHeight() / 2 - hint.outerHeight() / 2 - $(window).scrollTop() - 10;
hint.addClass(pos);
hint.css({
top: top,
left: -input.offset().left - hint.outerWidth() - 10
});
hint.show().animate({
left: left
}, o.hintEasingTime, o.hintEasing, function () {
setTimeout(function () {
hint.hide().remove();
}, o.hideHint);
});
} else if (pos === 'top') {
left = input.offset().left + input.outerWidth()/2 - hint.outerWidth()/2 - $(window).scrollLeft();
top = input.offset().top - $(window).scrollTop() - hint.outerHeight() - 20;
hint.addClass(pos);
hint.css({
top: -hint.outerHeight(),
left: left
}).show().animate({
top: top
}, o.hintEasingTime, o.hintEasing, function(){
setTimeout(function () {
hint.hide().remove();
}, o.hideHint);
});
} else /*bottom*/ {
left = input.offset().left + input.outerWidth()/2 - hint.outerWidth()/2 - $(window).scrollLeft();
top = input.offset().top - $(window).scrollTop() + input.outerHeight();
hint.addClass(pos);
hint.css({
top: $(window).height(),
left: left
}).show().animate({
top: top
}, o.hintEasingTime, o.hintEasing, function(){
setTimeout(function () {
hint.hide().remove();
}, o.hideHint);
});
}
}
},
_format: function( source, params ) {
if ( arguments.length === 1 ) {
return function() {
var args = $.makeArray( arguments );
args.unshift( source );
return $.validator.format.apply( this, args );
};
}
if ( arguments.length > 2 && params.constructor !== Array ) {
params = $.makeArray( arguments ).slice( 1 );
}
if ( params.constructor !== Array ) {
params = [ params ];
}
$.each( params, function( i, n ) {
source = source.replace( new RegExp( "\\{" + i + "\\}", "g" ), function() {
return n;
});
});
return source;
},
_destroy: function () {
},
_setOption: function ( key, value ) {
this._super('_setOption', key, value);
}
});