【学艺不精系列】几个常用JavaScript正则和方法

// 检查email
function isEmail(str) {
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
    return reg.test(str);
}
// 检查手机号码
function isMobile(str) {
    var reg = /^(1(3[0-9]|5[^4,\D]|8[0,5-9])\d{8})$/;
    return reg.test(str);
}
// 检查座机号码或传真
function isPhone(str) {
    var reg = /^(0[0-9]{2,3}\-{0,1}[0-9]{6,9}(\-{0,1}[0-9]{1,4}|))$/;
    return reg.test(str);
}

// 设置状态信息更新,msg:需要显示的信息。color:显示的颜色。
function fnTip(msg, color) {    
    $("#lblInfo").html("<font color='" + color + "'>" + msg + "</font>");
    if (color == "Red" || color == "red") {
        setTimeout(function () { $("#lblInfo").text("就绪"); }, 3000);
    } else if (color == "Blue" || color == "blue") {
        setTimeout(function () { $("#lblInfo").text("就绪"); }, 1000);
    } else {
        var ready = function () {
            $("#lblInfo").text("就绪");
            $(event.srcElement).unbind("mouseout", ready);
        }
        $(event.srcElement).bind("mouseout", ready);
    }
}

几个正则验证都较为严格。

posted on 2012-07-20 13:07  飞扬的尘埃  阅读(161)  评论(0编辑  收藏  举报

导航