js正則表達式:驗證郵箱格式、密碼復雜度、手機號碼、QQ號碼(轉)


原文地址:http://www.sucker-fly.com/archives/476

    $(function () {
                $("input[name='sub']").on("click", function () {
                    if (!isEmail($("input[name='email']").val())) {
                        $("span[name='email']").html("郵箱格式錯誤");
                        return false;
                    }
                    else {
                        $("span[name='email']").html("");
                    }
                    if (checkStrong($("input[name='password']").val()) < 3) {
                        $("span[name='password']").html("密碼太過簡單");
                        return false;
                    }
                    else {
                        $("span[name='password']").html("");
                    }
                    if (!isQQ($.trim($("input[name='qq']").val()))) {
                        $("span[name='qq']").html("請輸入正確的QQ號碼");
                        return false;
                    }
                    else {
                        $("span[name='qq']").html("");
                    }
                    if (!isPhone($.trim($("input[name='mnumber']").val()))) {
                        $("span[name='mnumber']").html("請輸入正確的手機號碼");
                        return false;
                    }
                    else {
                        $("span[name='mnumber']").html("");
                    }
                    return true;
                });
            });
            /**
            * 檢查字符串是否為合法QQ號碼
            * @param {String} 字符串
            * @return {bool} 是否為合法QQ號碼
            */
            function isQQ(aQQ) {
                var bValidate = RegExp(/^[1-9][0-9]{4,9}$/).test(aQQ);
                if (bValidate) {
                    return true;
                }
                else
                    return false;
            }
            /**
            * 檢查字符串是否為合法手機號碼
            * @param {String} 字符串
            * @return {bool} 是否為合法手機號碼
            */
            function isPhone(aPhone) {
                var bValidate = RegExp(/^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/).test(aPhone);
                if (bValidate) {
                    return true;
                }
                else
                    return false;
            }
            /**
            * 檢查字符串是否為合法email地址
            * @param {String} 字符串
            * @return {bool} 是否為合法email地址
            */
            function isEmail(aEmail) {
                var bValidate = RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(aEmail);
                if (bValidate) {
                    return true;
                }
                else
                    return false;
            }
            /**
            * 檢查字符串是否是整數
            * @param {String} 字符串
            * @return {bool} 是否是整數
            */
            function isInteger(s) {
                var isInteger = RegExp(/^[0-9]+$/);
                return (isInteger.test(s));
            }
            /*
                判斷字符類型
            */
            function CharMode(iN) {
                if (iN >= 48 && iN <= 57) //數字  
                    return 1;
                if (iN >= 65 && iN <= 90) //大寫字母  
                    return 2;
                if (iN >= 97 && iN <= 122) //小寫  
                    return 4;
                else
                    return 8; //特殊字符  
            }
            /*
                統計字符類型
            */
            function bitTotal(num) {
                modes = 0;
                for (i = 0; i < 4; i++) {
                    if (num & 1) modes++;
                    num >>>= 1;
                }
                return modes;
            }
            /*
                返回密碼的強度級別
            */
            function checkStrong(sPW) {
                if (sPW.length <= 4)
                    return 0; //密碼太短  
                Modes = 0;
                for (i = 0; i < sPW.length; i++) {
                    //測試每一個字符的類別並統計一共有多少種模式.  
                    Modes |= CharMode(sPW.charCodeAt(i));
                }
                return bitTotal(Modes);
            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM