validate.js 3.55 KB
/**
 * 验证组件
 * 根据传入规则和信息,验证元素的值是否符合规则,给出相应的信息提示。
 * 使用:
 * 1、$(parent).validate(param)--parent为验证元素的父元素,验证元素需要添加requireded类
 * 2、参数param:{
 * value: '', //需要验证的值
  rules: [], //规则数组
  style: {}, //错误提示信息的样式
  errorMsg: '', //无rules时,想直接显示错误信息使用
  isShowError: false, //直接显示错误提示信息errorMsg
  isHideError: false //隐藏错误提示信息
  }
 */
define('validate', ['components/common', 'jquery', 'utils'], function () {
  var eles = 'input.requireded,select.requireded,textarea.requireded';

  var _Validate = function (ele, settings) {
    this.ele = ele;
    this.rules = settings.rules;
    this.style = settings.style;
  }

  _Validate.prototype = {
    bindEvent: function () {
      var that = this;
      $(this.ele).off('change.validate', eles)
        .on('change.validate', eles, function () {
          var $this = $(this);
          var value = $this.val();
          that.valid(value);
        })
    },
    valid: function (value) {
      value = value ? value.replace(/(\s*$)/g, "") : '';
      
      var isValid = true;
      var _u = require('utils');

      if (this.rules.length) {
        var rule = '';
        var msg = '';
        for (var i = 0; i < this.rules.length; i++) {
          rule = this.rules[i].rule ? this.rules[i].rule : /[^\s]+/; //默认规则“非空”
          msg = this.rules[i].msg ? this.rules[i].msg : '';
          var ruleReg = new RegExp(rule);
          if (!ruleReg.test(value) && !(_u.isBoolean(value) && value)) { ////value值boolean型且为true时,默认是正确的
            isValid = false;
            this.createError(msg);
            break;
          }
        }
      } else {
    	this.hideError();
      }

      if (isValid) {
    	this.hideError();
      }
    },
    createError: function (msg) {
      var style = this.style;
      var $container = $(this.ele);
      var errorExist = $container.children('.error-tip').length;

      if (!errorExist) {
        $container.addClass('has-error');
        $container.append('<span class="error-tip">\
            <i class="iconfont icon-warn main-color"></i>\
            <span class="tip-msg">' + msg + '</span>\
            <span class="clean-error" >×</span>\
          </span>');
        var that = this;
        $container.children('.error-tip').css(style).find('.clean-error').on('click',function () {
          that.hideError();
          return false;
        });
      } else {
        $container.addClass('required has-error')
          .children('.error-tip').show().css(style)
          .children('.tip-msg').html(msg);
      }
    },
    showError:function(msg){
    	this.createError(msg);
    },
    hideError:function(){
    	$(this.ele).removeClass('has-error').children('.error-tip').hide();
    }
  }

  $.fn.validate = function (settings) {
    var defaultSetting = {
      rules: [],
      style: {},
      errorMsg:'',
      isShowError:false,
      isHideError:false
    };
    var cfg = $.extend(defaultSetting, settings);
    var _validate = new _Validate(this, cfg);
    var value = cfg.value !== undefined ? cfg.value : $(_validate.ele).find('input.requireded,select.requireded,textarea.requireded').eq(0).val();
    
    if(cfg.isShowError){
    	return _validate.showError(cfg.errorMsg);
    }
    if(cfg.isHideError){
    	return _validate.hideError();
    }

    !$(this).children('.error-tip').length && _validate.bindEvent();
    _validate.valid(value);
  }
});