官术网_书友最值得收藏!

6.4 應用正則表達式對用戶注冊信息進行驗證

【例6.6】通過正則表達式對用戶注冊信息的合理性進行判斷,對用戶輸入的郵編、電話號碼、郵箱地址和網址的格式進行判斷。本例中應用正則表達式和JavaScript腳本,判斷用戶輸入信息的格式是否正確。實例代碼如下:(實例位置:光盤\TM\sl\6\6)

首先,在index.php頁面中通過Script腳本調用js腳本文件check.js,創建form表單,實現會員注冊信息的提交,并應用onSubmit事件調用chkreg()方法對表單元素中的數據進行驗證,將數據提交到index_ok.php文件中。index.php的關鍵代碼如下:

        <script src="js/check.js"></script>
        <form name="reg_check"method="post"action="index_ok.php"onSubmit="return chkreg(reg check, 'all')">
        <table width="550" height="270" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td height="30"><div align="right">郵政編碼:</div></td>
                <td height="30" colspan="2" align="left">&nbsp;
                    <input type="text"name="postalcode"size="20"onBlur="chkreg(reg check,2)">
                    <div id="check postalcode"style="color:#F1B000"></div>
                </td>
            </tr>
            <tr>
                <td height="30"><div align="right">E-mail:</div></td>
                <td height="30" colspan="2" align="left">&nbsp;
                <input type="text" name="email" size="20" onBlur="chkreg(reg_check,4)">
                    <font color="#999999">請務必正確填寫您的郵箱</font>
                    <div id="check email"style="color:#F1B000"></div>
                </td>
            </tr>
            <tr>
                <td height="30" align="right">固定電話:</td>
                <td height="30" colspan="2" align="left">&nbsp;
                    <input type="text"name="gtel"size="20"  onBlur="chkreg(reg check,6)">
                    <font color="#999999"><div id="check gtel"style="color:#F1B000"></div></font></td>
            </tr>
            <tr>
                <td height="30"><div align="right">移動電話:</div></td>
                <td height="30" colspan="2" align="left">&nbsp;
                      <input type="text"name="mtel"size="20"  onBlur="chkreg(reg check,5)">
                    <div id="check_mtel" style="color:#F1B000"></div></td>
            </tr>
            <tr>
                <td width="100"height="30"><input type="image"  src="images/bg_09.jpg"></td>
                <td width="340"><img src="images/bg_11.jpg" width="56" height="30" onClick="reg_check.reset()"
        style="cursor:hand"/></td>
            </tr>
        </table>
        </form>

在check.js腳本文件中,創建自定義方法,應用正則表達式對會員注冊的電話號碼和郵箱進行驗證。其關鍵代碼如下:

        function checkregtel(regtel){
            var str=regtel;
            var Expression=/^13(\d{9})$|^18(\d{9})$|^15(\d{9})$/;                   //驗證手機號碼
            var objExp=new RegExp(Expression);
            if(objExp.test(str)==true){
                return true;
            }else{
                return false;
            }
        }
        function checkregtels(regtels){
            var str=regtels;
            var Expression=/^(\d{3}-)(\d{8})$|^(\d{4}-)(\d{7})$|^(\d{4}-)(\d{8})$/; //驗證座機號碼
            var objExp=new RegExp(Expression);
            if(objExp.test(str)==true){
                return true;
            }else{
                return false;
            }
        }
        function checkregemail(emails){
            var str=emails;
            var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;       //驗證郵箱地址
            var objExp=new RegExp(Expression);
            if(objExp.test(str)==true){
                return true;
            }else{
                return false;
            }
        }

運行結果如圖6.3所示。

圖6.3 應用正則表達式對用戶注冊信息進行驗證

注意

在本例中通過正則表達式對表單提交的數據進行驗證,在JavaScript腳本中,應用onBlur事件調用對應的方法對表單提交的數據直接進行驗證,并通過div標簽返回結果。

主站蜘蛛池模板: 安西县| 漳浦县| 德庆县| 高雄市| 奎屯市| 北川| 阿拉善盟| 庆安县| 娄烦县| 常州市| 平舆县| 商丘市| 项城市| 新干县| 泰顺县| 清徐县| 巴楚县| 沁阳市| 高平市| 娱乐| 清丰县| 岐山县| 青田县| 金堂县| 固安县| 卢氏县| 阳西县| 浮梁县| 平塘县| 临汾市| 双牌县| 昌平区| 宁国市| 会同县| 滦南县| 岳西县| 余江县| 庆安县| 余姚市| 宿松县| 和顺县|