Init
This commit is contained in:
65
js/sxValidator.js
Normal file
65
js/sxValidator.js
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
function sxValidator(opts){
|
||||
|
||||
var self = this;
|
||||
this.valid = false;
|
||||
|
||||
this.options = opts;
|
||||
this.$form = typeof opts.$el == 'string' ? $(opts.$el) : opts.$el;
|
||||
|
||||
this.init = function(){
|
||||
this.$form.submit(function(e){
|
||||
if(self.valid){
|
||||
//if(this.$form.hasClass('_preorder')) yandexReachGoal('zakazkorzina');
|
||||
return true;
|
||||
}
|
||||
e.preventDefault();
|
||||
self.validate();
|
||||
});
|
||||
|
||||
this.$form.on('click keyup', '.input-error', function(){
|
||||
self.removeError($(this));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
this.validate = function(){
|
||||
var flag = true;
|
||||
this.$form.find('[data-format]').each(function(){
|
||||
var $input = $(this);
|
||||
var val = $input.val();
|
||||
var re = new RegExp($input.data('format'));
|
||||
if(re.test(val)) return;
|
||||
var msg = $input.data('notice') ? $input.data('notice') : 'Это поле необходимо заполнить';
|
||||
self.showError($input, msg);
|
||||
flag = false;
|
||||
return false;
|
||||
});
|
||||
|
||||
if(flag){
|
||||
this.valid = true;
|
||||
this.$form.submit();
|
||||
}
|
||||
}
|
||||
|
||||
this.showError = function($el, msg){
|
||||
$el.addClass('input-error').parents('.form-group').addClass('has-error');
|
||||
$el.popover({
|
||||
html:true,
|
||||
content: msg,
|
||||
placement: 'bottom'
|
||||
});
|
||||
$el.popover('show');
|
||||
$el.focus();
|
||||
}
|
||||
|
||||
this.removeError = function($el){
|
||||
$el.removeClass('input-error').parents('.form-group').removeClass('has-error');
|
||||
$el.popover('destroy');
|
||||
}
|
||||
|
||||
var __construct = function(that) {
|
||||
that.init();
|
||||
}(this);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user