詳解JavaScript中的表單驗證_基礎知識

來源:互聯網
上載者:User

 表單驗證用於發生在伺服器,用戶端已經輸入所有必要的資料,然後按下提交按鈕之後。如果一些已被輸入的用戶端的資料的已在錯誤形式或者被簡單地丟失,則伺服器將必須的所有資料發送回用戶端,並請求的形式以正確的資訊重新提交。這是一個漫長的過程,會增加伺服器負擔。

JavaScript中,提供了一種方法將其發送到web伺服器之前驗證用戶端的電腦上的形式的資料。表單驗證通常執行兩種方式。

  1.     基本驗證 - 首先,該表必須進行檢查,以確保資料輸入的需要將其每一個表單欄位。這將通過表格的每個欄位只需要迴圈,並檢查資料。
  2.     資料格式驗證 - 其次,該被輸入的資料必須檢查正確格式和值。這將需要放置更多的邏輯來測試資料的正確性。

我們將舉一個例子來瞭解驗證的過程。下面是簡單的形式進行:

<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>

基本表單驗證:

首先,我們將展示如何做一個基本的表單驗證。在上面的表格要求validate()函數來驗證資料在onsubmit事件發生。以下是validate()函數的實現:

<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>

 
資料格式驗證:

現在,我們將看到我們如何將其提交到Web伺服器之前,驗證我們輸入的表單資料。

這個例子說明了如何驗證輸入的電子郵件地址,這意味著電子郵件地址必須至少包含一個@符號和一個點(.)。此外,@絕不能是電子郵件地址的第一個字元,最後點必須在@符號後面的一個字元:

<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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.