post

工作中常用的Javascript

常用验证表单的Javascript:

//验证Email
function checkEmail(mail) {
    if (mail.length > 0 && mail.length < 101) {
        var regex = /^[_\.0-9a-zA-Z+-]+@([0-9a-zA-Z]+[0-9a-zA-Z-]*\.)+[a-zA-Z]{2,4}$/;
        if (regex.exec(mail)) {
            return true;
        }
    }
    return false;
}
//验证邮政编码
function checkZip(zip) {
    var regex = /^[0-9]{6}$/;
    if (regex.exec(zip)) {
        return true;
    }
    return false;
}
//验证身份证
function checkIdCard(idcard) {
    var regex = /^[0-9xX]{15,18}$/;
    if (regex.exec(idcard)) {
        return true;
    }
    return false;
}
//验证固话
function checkTel(tel) {
    var regex = /^[0-9]{3,4}-[0-9]{7,8}$/;
    if (regex.exec(tel)) {
        return true;
    }
    return false;
}
//验证手机
function checkMobile(mobile) {
    var regex = /^[0-9]{11}$/;
    if (regex.exec(mobile)) {
        return true;
    }
    return false;
}
//验证QQ
function checkQQ(qq) {
    qq = $.trim(qq);
    if (qq.length > 0 && qq.length < 101) {
        var regex = /^[0-9]{1,12}$/;
        if (regex.exec(qq)) {
            return true;
        } else {
            return checkEmail(qq);
        }
    }
    return false;
}
//验证名称中文,英文或数字
function checkUname(name) {
    var regex = /^[a-zA-Z0-9\u4e00-\u9fa5]{2,10}$/;
    if (regex.exec(name)) {
        return true;
    }
    return false;
}
function checkUname(name,start,end) {
    var regex = new RegExp("^[a-zA-Z0-9\\u4e00-\\u9fa5]{"+start+","+end+"}$");
    if (regex.exec(name)) {
        return true;
    }
    return false;
}
//验证网址
function checkSiteUrl(url) {
    var regex = /^http:\/\/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}(:[0-9]{1,5})?$/;
    if (regex.exec(url)) {
        if (url.length < 201) {
            var istrue = false;
            $.ajax({
                type: "post",
                url: "register.aspx",
                dataType: "html",
                async: false,
                data: "url=" + escape(url) + "&t=checkUrl",
                cache: false,
                success: function(data) {
                    if (data == "can") {
                        istrue = true;
                    }
                }
            });
            if (istrue) {
                return true;
            }
        }
    }
    return false;
}
· 663 次浏览