JavaScript 表單有效性驗(yàn)證

2018-09-28 18:05 更新

表單有效性驗(yàn)證

當(dāng)用戶輸完了所有必要的數(shù)據(jù)并且點(diǎn)擊了提交按鈕,接著就會(huì)進(jìn)行表單的有效性進(jìn)行驗(yàn)證,表單驗(yàn)證通常發(fā)生在服務(wù)器端。如果用戶輸入的一些數(shù)據(jù)存在錯(cuò)誤,或者沒有輸入數(shù)據(jù),服務(wù)器就必須返回給客戶端所有的數(shù)據(jù),并且要求用戶重新提交正確的數(shù)據(jù)。這個(gè)過程確實(shí)是處理比較費(fèi)時(shí),而且給服務(wù)器造成很大壓力。

JavaScript 提供了一種方式在客戶端提交數(shù)據(jù)到服務(wù)器之前驗(yàn)證表單數(shù)據(jù)的有效性。通常利用兩個(gè)函數(shù)來驗(yàn)證表單數(shù)據(jù)的有效性。

  • 基本有效性驗(yàn)證: 首先,表單必須確保所有需要輸入的數(shù)據(jù)的區(qū)域有數(shù)據(jù)。這個(gè)需要循環(huán)的對(duì)表單中需要輸入數(shù)據(jù)的區(qū)域進(jìn)行檢驗(yàn)數(shù)據(jù)是否為空。
  • 數(shù)據(jù)格式驗(yàn)證: 其次,被輸入的數(shù)據(jù)必須要檢查它的格式和值是否正確。這就需要更多的邏輯測(cè)試。

我們舉個(gè)例子來說明有效性驗(yàn)證的過程。如下是個(gè)簡(jiǎn)單的例子:

    <html>
    <head>
    <title>Form Validation</title>
    <script type="text/javascript">
    <!--
    // Form validation code will come here.
    //-->
    </script>
    </head>
    <body>
    <form action="/cgi-bin/test.cgi" name="myForm"  
          onsubmit="return(validate());">
    <table cellspacing="2" cellpadding="2" border="1">
    <tr>
    <td align="right">Name</td>
    <td><input type="text" name="Name" /></td>
    </tr>
    <tr>
    <td align="right">EMail</td>
    <td><input type="text" name="EMail" /></td>
    </tr>
    <tr>
    <td align="right">Zip Code</td>
    <td><input type="text" name="Zip" /></td>
    </tr>
    <tr>
    <td align="right">Country</td>
    <td>
    <select name="Country">
    <option value="-1" selected>[choose yours]</option>
    <option value="1">USA</option>
    <option value="2">UK</option>
    <option value="3">INDIA</option>
    </select>
    </td>
    </tr>
    <tr>
    <td align="right"></td>
    <td><input type="submit" value="Submit" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>

基本表單驗(yàn)證

首先我們會(huì)展示如何進(jìn)行基本的表單有效性驗(yàn)證。上面的代碼中我們調(diào)用了 validate() 函數(shù)驗(yàn)證數(shù)據(jù)有效性,當(dāng)事件 onsubmit 發(fā)生的時(shí)候。下面是對(duì) validate() 函數(shù)的實(shí)現(xiàn)。

    <script type="text/javascript">
    <!--
    // Form validation code will come here.
    function validate()
    {

       if( document.myForm.Name.value == "" )   
       {
         alert( "Please provide your name!" );
         document.myForm.Name.focus() ;
         return false;
        }
     if( document.myForm.EMail.value == "" )
    {
     alert( "Please provide your Email!" );
     document.myForm.EMail.focus() ;
     return false;
    }
    if( document.myForm.Zip.value == "" ||
           isNaN( document.myForm.Zip.value ) ||
           document.myForm.Zip.value.length != 5 )
    {
     alert( "Please provide a zip in the format #####." );
     document.myForm.Zip.focus() ;
     return false;
    }
    if( document.myForm.Country.value == "-1" )
    {
     alert( "Please provide your country!" );
     return false;
    }
    return( true );
    }
    //-->
    </script>

數(shù)據(jù)格式有效性驗(yàn)證

接下來我們將會(huì)演示在將數(shù)據(jù)提交到服務(wù)器端之前我們?nèi)绾悟?yàn)證數(shù)據(jù)的有效性。

這個(gè)例子演示如何驗(yàn)證用戶輸入的郵箱地址的有效性,因?yàn)檩斎氲泥]箱格式中必須包含 @ 符號(hào)和一個(gè)點(diǎn)號(hào)(.)。并且,符號(hào) @ 不能作為作為郵箱地址的第一個(gè)字符,在 @ 符號(hào)之后和點(diǎn)號(hào)之前至少要有一個(gè)字符:

    <script type="text/javascript">
    <!--
    function validateEmail()
    {

    var emailID = document.myForm.EMail.value;
    atpos = emailID.indexOf("@");
    dotpos = emailID.lastIndexOf(".");
    if (atpos < 1 || ( dotpos - atpos < 2 )) 
    {
       alert("Please enter correct email ID")
       document.myForm.EMail.focus() ;
       return false;
    }
    return( true );
    }
    //-->
    </script>
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)