首頁  >  文章  >  web前端  >  js中判斷使用者輸入的值是否為空的簡單實例_javascript技巧

js中判斷使用者輸入的值是否為空的簡單實例_javascript技巧

WBOY
WBOY原創
2016-05-16 17:07:401042瀏覽

在js中判斷使用者輸入的值是否為空,這是大家用得非常多的. 這沒有什麼好寫的. 而我卻寫了. 原因只是自以為是的認為我的這些代碼寫得不錯,供大家參考一下.

這是摘自的我一個專案的中的用戶註冊頁面.對於大多數人來說,這都幾乎是100%經歷過的.
貼代碼吧,這些代碼都是用js寫的.不難,很容易看懂. 看的時候,只要區別兩個js類就行了.
前台頁碼:reguser.aspx

複製程式碼 程式碼如下:




   
                  <br> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <br>        <meta name="CODE_LANGUAGE" content="C#"> <br>  Script>  <br>        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <br>        <link rel="style /EnterpriseWebSite.css"> <br>        <script src="/js/CommonFunction.js"></script><br>                 reg.userName    = ' ' ;<br>            reg.password    = '';<br>               = '';<br>            reg.answer = '';<br>            reg.url          ;<br>            reg.sex    = 1;<br>             = '';<br>            reg.mobile    = '';<br>         <br>            reg.address    = '';<br>               = null;<br><br>             form = document.forms[0];<br>               var comFun    = new commonFunction();               {<br>              }<br><br>                    { <br>        { <br>                   密碼輸入不一致');<br>                  comFun.$getElementById('txtConfirmPassword  ;<br>                }<br>                reg.userName    = comFun.$getElementById('txtUserName');               reg .question    = comFun.$getElementById(' txtQuestion') ;<br>                reg.answer        = comFun.$getElementById('         = comFun.$getElementById('txtUrl');<br>                );<br>                reg.tel            = comFun.$get mobile        = comFun.$getElementById('txtMobile');<br>             ');<br>                reg.address        = comFun.$getElement    = comFun.$getElementById('txtPostalcode');<br><br>                變數項'sex');<br>                var eL    = es.length;<br>                  {<br>         {<br>         <br>                              reg.sex     = e.value;<br>                      }<br>                }<br><br>     .value、reg.password.value、reg.question.value、reg.answer.value、reg.url.value、reg.sex.value、reg.email.value、reg.tel.value、reg.mobile.value , reg.qq.value, reg.address.value, reg.postalcode.value,callback_Reg);<br>            }                var rv    = res.value ; <br>                            '註冊成功!');<br>                    window.location.href='/Default.aspx ' ;                else<br>                {<br>                    alert('有错误发生,注册失败!有可能是用户名或者域名被别人注册过了!');<br>                }<br>            }<br><br>            // 检测用户名的域名是否被其它用户注册过了<br>            function checkIsRegistered(obj, errorS, t)<br>            {<br>                var v    = obj.value;<br><br>                var rv    = RegUser.CheckIsRegistered(v, t).value;<br>                if(rv)<br>                {<br>                    alert(errorS);<br>                    obj.select();<br>                    return false;<br>                }<br>            }<br>        </script><br>    </head><br>    <body MS_POSITIONING="GridLayout"><br>        <form id="Form1" method="post" runat="server"><br>            <table width="98%" border="0" align="center" cellpadding="3" cellspacing="0"><br>                <tr><br>                    <td width="16%"><div align="right">用户名:</div><br>                    </td><br>                    <td width="84%"><input type="text" id="txtUserName" onblur="checkIsRegistered(this, '该用户名已经被注册,请使用其它的!', 1)" maxlength="15" isRequired="true" isValidate="true" errorSForEmpty="用户名不能为空!" errorSForValidate="匹配出错!以字母开头,允许3-16字节,允许字母数字下划线以及许可的安全符号!" validatePattern="/^[a-zA-Z][a-zA-Z0-9_$%]{2,15}$/" title="用户名必须填写!"><font color="#ff0000">*</font></td><br>                </tr><br>                <tr><br>                    <td><div align="right">密码:</div><br>                    </td><br>                    <td><input type="password" id="txtPassword" maxlength="15" isRequired="true" isValidate="true" errorSForEmpty="密码不能为空!" errorSForValidate="匹配出错!要求3-16字节,允许字母数字下划线以及许可的安全符号!!" validatePattern="/^[a-zA-Z0-9_$%]{2,15}$/" title="密码必须填写!"><font color="#ff0000">*</font></td><br>                </tr><br>                <tr><br>                    <td><div align="right">确认密码:</div><br>                    </td><br>                    <td><input type="password" id="txtConfirmPassword" isRequired="true" errorSForEmpty="确认密码不能为空!"><font color="#ff0000">*</font></td><br>                </tr><br>                <tr><br>                    <td><div align="right">密码提示问题:</div><br>                    </td><br>                    <td><input type="text" id="txtQuestion" maxlength="50" isRequired="true" isValidate="true" errorSForEmpty="密码提示问题没有填写!" errorSForValidate="长度必须在8-50个字之间且不得有空格!" validatePattern="/\S{8,50}/" title="密码提示问题必须填写!"><font color="#ff0000">*</font></td><br>                </tr><br>                <tr><br>                    <td><div align="right">密码问题答案:</div><br>                    </td><br>                    <td><input type="text" id="txtAnswer" maxlength="50" isRequired="true" isValidate="true" errorSForEmpty="密码问题答案没有填写!" errorSForValidate="长度必须在8-50个字之间且不得有空格!" validatePattern="/\S{8,50}/" title="密码问题答案必须填写!"><font color="#ff0000">*</font></td><br>                </tr><br>                <tr><br>                    <td><div align="right">站点Url:</div><br>                    </td><br>                    <td><input type="text" id="txtUrl" onblur="checkIsRegistered(this, '该Url已经被注册,请使用其它的!', 2)" maxlength="20" isRequired="true" isValidate="true" errorSForEmpty="站点Url不能为空!" errorSForValidate="站点Url格式不对!" validatePattern="/^[a-zA-Z0-9]{1,20}$/" title="站点Url必须填写!"><font color="#ff0000">*</font></td><br>                </tr><br>                <tr><br>                    <td><div align="right">性别:</div><br>                    </td><br>                    <td><input type="radio" id="boy" name="sex" value="1" checked>男  <input type="radio" id="girl" name="sex" value="0">女</td><br>                </tr><br>                <tr><br>                    <td><div align="right">Email:</div><br>                    </td><br>                    <td><input type="text" id="txtEmail" isValidate="true" errorSForValidate="Email格式不正确!" validatePattern="/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/"></td><br>                </tr><br>                <tr><br>                    <td><div align="right">固定电话:</div><br>                    </td><br>                    <td><input type="text" id="txtTel" isRequired="true" isValidate="true" errorSForEmpty="固定电话不能为空!" errorSForValidate="固定电话格式不对!请使用0592-5555555的格式!" validatePattern="/^(\d{3}-|\d{4}-)?(\d{8}|\d{7})$/" title="固定电话必须填写!"><font color="#ff0000">*</font></td><br>                </tr><br>                <tr><br>                    <td><div align="right">移动电话:</div><br>                    </td><br>                    <td><input type="text" id="txtMobile" isValidate="true" errorSForValidate="移动电话格式不正确!" validatePattern="/^1\d{10}$/"></td><br>                </tr><br>                <tr><br>                    <td><div align="right">QQ:</div><br>                   ForValidate="QQ 格式不正確!" validatePattern="/^[1-9] *[1-9][0-9]*$/"><br>                <br>      div><br>                    <br>        🎜>                <br>              tdtd><divalign>郵編:</divalign>

                   
  txtPostalcode" maxlength="6" isValidate="true" errorSForValidate="郵編不正確!" validatePattern="/^d{6}/">
                                作業:
                   
         click="btnSubmit_onclick()">  

               
             
/body>



在上面的程式碼中,包含了一個CommonFunction.js文件,以下是他的內容:




複製程式碼

程式碼如下:

/**************************************************** *******
*
*    公用js函數
*
************************* **********************************/
function commonFunction()
{
    // 檢查數值是否為null 或為空白 🎜> {
        var flag    = true;
        for(var i=0; i    var e    = obj.item(i);
            if (e .isRequired)
            {   
               {
                   alert(e.errorSForEmpty);
                    flag    = false;
                    中斷中;
         
            if(e.isValidate)
            {                {
                   alert(e.errorSForValidate);
                     e.focus();
                      中斷;
                }
         >        }

        return flag;
    }

      {
        var v    = e. value;
        if(v!='')
        {
        🎜>    }

    // 正規表達式驗證
    this.checkReg    = function(pattern, value)
    {
       var reg    = new RegExp(pattern);
        if( !reg.test(value))
        {
        🎜>
    // 傳回id 的Element By id 物件。
    this.$ getElementById    = function(id)
    {
        var e  ='undefined')
        {
            return e;
        }

       

    // 傳回 id 的 Element By name 物件。
    this.$getElementsByName    = function(id)
    {
        var e  !='undefined')
        {
            return e;
        }

       🎜>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn