ASP.NET验证控件
? ? 在此過程中房間的收費制度時,。為了驗證文本框是否為空、用戶存在、合法等等。我們都要單獨寫代碼。學習了ASP.NET的驗證控件,省了非常多事。
? ? ASP.NET能夠輕松實現對用戶輸入的驗證。
讓我們好好回想一下ASP.NET公有的六種驗證控件。
1、基本驗證控件:
(1)RequiredFieldValidator:限制空字段(必須字段驗證) 用于檢查是否有輸入值
樣例:比方登陸時。須要驗證輸入的username和password是否為空
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUserName" ErrorMessage="username必須輸入"></asp:RequiredFieldValidator>(2)基本比較控
a、CompareValidator:比較兩個字段?比較驗證) 按設定比較兩個輸入?
樣例:當我們改動password時。第一次輸入password和第二次輸入的是否一致,能夠用到這個控件。
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword1" ControlToValidate="txtPassword2" ErrorMessage="兩次password不一致"></asp:CompareValidator>b、RangeValidator:檢查指定范圍(范圍驗證) 輸入是否在指定范圍
比如:輸入信息的范圍大小
<ASP:RangeValidator id="Vaidator_ID" Runat="Server"?controlToValidate="要驗證的控件ID" type="Integer" MinimumValue="最小值" MaximumValue="最大值" errorMessage="錯誤信息"Display="Static|Dymatic|None"></ASP:RangeValidator>比如:輸入信息的范圍大小
(3)RegularExpressionValidator:使用正則表達式檢查值(正則表達式驗證) 正則表達式驗證控件?
(4)CustomValidator:通過client或server函數檢查值(自己定義驗證) 自己定義驗證控件?
<ASP:CustomValidator id="Validator_ID" RunAt="Server" controlToValidate="要驗證的控件"onServerValidateFunction="驗證函數"errorMessage="錯誤信息"Display="Static|Dymatic|None"></ASP: CustomValidator >
3、驗證總結控件
?(1)ValidationSummary:在頁面中列出全部控件的驗證錯誤(驗證總結) 總結驗證結果?
<ASP:ValidationSummary id="ValSum" RunAT="Server" HeaderText="所以發現的錯誤是:"DiaplayMode="SingleParagraph"></ASP: ValidationSummary >?<span style="font-family: KaiTi_GB2312; background-color: rgb(255, 255, 255);">?</span>
Page.IsValid屬性:
<scrip language="C#" runat="server"> Void subbtn(Object Src,EventArge E) { if(Page.IsValid==true) <span style="white-space:pre"> </span>{lblMessage.Text="頁面有效";} } </script> Page:當前的整個頁面,不論什么一個沒有通過驗證它的值就為false.總結一下:
? ? ?每個驗證控件運行特定類型的驗證,并在驗證失敗時展現自己的自定義短信提示。驗證控制我們的發展提供了一個非常大的幫助,我們并不需要太多的代碼,您可以驗證各種認證。
總結
以上是生活随笔為你收集整理的ASP.NET验证控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【UVA】10012 - How Big
- 下一篇: bootstrap的验证和确认对话框