前端相关校验

阅读更多

1,文本框失去焦点时若有值则trim

$('#identityTF').blur(function (e) {
            console.log('blur');
            //去掉首尾的空格
            var eventTarget = e.srcElement || e.target || e.toElement;
            if (eventTarget) {
                var identityTmp = eventTarget.value;
                identityTmp = util.trim(identityTmp);
                if (identityTmp) {
                    eventTarget.value = identityTmp;
                }
            }
})

 

 

2,文本框聚焦或有键按下时清除错误样式

/***
         * 清除错误样式
         * @param event
         */
        var cleanErrorStyle = function (event) {
            var errorClass = 'has-error-inp';
            var eventTarget = event.srcElement || event.target || event.toElement;
            $(eventTarget).removeClass(errorClass);
        };
        $('input[type=text]').keydown(cleanErrorStyle).focus(cleanErrorStyle);

 

 

 

 

3,如何在事件处理方法中获取事件源(控件)

方式一:

var eventTarget = e.srcElement || e.target || e.toElement;

封装成方法:

/***
 * 获取事件源
 * @param evt
 * @returns {*|Object}
 */
com.whuang.hsj.getSrcElement = function (evt) {
    var event = window.event || evt;//兼容ie7 8
    var eventTarget = event.srcElement || event.target || event.toElement || event.currentTarget;
    return eventTarget;
};

 

 

方式二:

var val=$(this).val();

 4,判断是否是邮箱

方式一:

var isEmail= function (email2) {
            if (typeof email2 == 'object') {
                email2 = email2.value;
            }
            var pattern = /^\w+([_+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            return pattern.test(email2);
        };
var emi='@[email protected]';
console.log(isEmail(emi));

 方式二:

/**
 * 邮箱校验
 *
 */
emailCheck = function (email) {
    var emailPattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9])+\.[a-zA-Z]{2,3}$/;
    return emailPattern.test(email);
};

 

 

你可能感兴趣的:(校验,trim,清除错误样式,clean,error,style)