NET驗證控制項驗證用戶輸入的數據,以確保不存儲無用的、未經驗證的或矛盾的數據。
ASP.NET提供以下驗證控制項:
- RequiredFieldValidator
- RangeValidator
- CompareValidator
- RegularExpressionValidator
- CustomValidator
- ValidationSummary
BaseValidator Class
驗證控制項類繼承自BaseValidator類,因此它們繼承其屬性和方法。因此,查看這個基類的屬性和方法會有幫助,這些屬性和方法對於所有驗證控制項都是通用的:
Members | Description |
---|---|
ControlToValidate | Indicates the input control to validate. |
Display | Indicates how the error message is shown. |
EnableClientScript | Indicates whether client side validation will take. |
Enabled | Enables or disables the validator. |
ErrorMessage | Indicates error string. |
Text | Error text to be shown if validation fails. |
IsValid | Indicates whether the value of the control is valid. |
SetFocusOnError | It indicates whether in case of an invalid control, the focus should switch to the related input control. |
ValidationGroup | The logical group of multiple validators, where this control belongs. |
Validate() | This method revalidates the control and updates the IsValid property. |
RequiredFieldValidator Control
RequiredFieldValidator控制項確保RequiredField不爲空。它通常綁定到文本框以強制輸入到文本框中。
控制項的語法如下所示:
<asp:RequiredFieldValidator ID="rfvcandidate" runat="server" ControlToValidate ="ddlcandidate" ErrorMessage="Please choose a candidate" InitialValue="Please choose a candidate"> </asp:RequiredFieldValidator>
RangeValidator Control
RangeValidator控制項驗證輸入值是否在預定範圍內。
它有三個特定的屬性:
Properties | Description |
---|---|
Type | It defines the type of the data. The available values are: Currency, Date, Double, Integer, and String. |
MinimumValue | It specifies the minimum value of the range. |
MaximumValue | It specifies the maximum value of the range. |
控制項的語法如下所示:
<asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass" ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" MinimumValue="6" Type="Integer"> </asp:RangeValidator>
CompareValidator Control
CompareValidator控制項將一個控制項中的值與固定值或另一個控制項中的值進行比較。
它具有以下特定屬性:
Properties | Description |
---|---|
Type | It specifies the data type. |
ControlToCompare | It specifies the value of the input control to compare with. |
ValueToCompare | It specifies the constant value to compare with. |
Operator | It specifies the comparison operator, the available values are: Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and DataTypeCheck. |
控制項的基本語法如下:
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator"> </asp:CompareValidator>
RegularExpressionValidator
RegularExpressionValidator允許通過與正則表達式的模式匹配來驗證輸入文本。正則表達式在ValidationExpression屬性中設置。
下表總結了正則表達式常用的語法結構:
Character Escapes | Description |
---|---|
\b | Matches a backspace. |
\t | Matches a tab. |
\r | Matches a carriage return. |
\v | Matches a vertical tab. |
\f | Matches a form feed. |
\n | Matches a new line. |
\ | Escape character. |
除了單字符匹配外,還可以指定一類可以匹配的字符,稱爲元字符。
Metacharacters | Description |
---|---|
. | Matches any character except \n. |
[abcd] | Matches any character in the set. |
[^abcd] | Excludes any character in the set. |
[2-7a-mA-M] | Matches any character specified in the range. |
\w | Matches any alphanumeric character and underscore. |
\W | Matches any non-word character. |
\s | Matches whitespace characters like, space, tab, new line etc. |
\S | Matches any non-whitespace character. |
\d | Matches any decimal character. |
\D | Matches any non-decimal character. |
可以添加量詞來指定字符出現的次數。
Quantifier | Description |
---|---|
* | Zero or more matches. |
+ | One or more matches. |
? | Zero or one matches. |
{N} | N matches. |
{N,} | N or more matches. |
{N,M} | Between N and M matches. |
控制項的語法如下所示:
<asp:RegularExpressionValidator ID="string" runat="server" ErrorMessage="string" ValidationExpression="string" ValidationGroup="string"> </asp:RegularExpressionValidator>
CustomValidator
CustomValidator控制項允許爲客戶端和伺服器端驗證編寫特定於應用程式的自定義驗證例程。
客戶端驗證通過ClientValidationFunction屬性完成。客戶端驗證例程應使用腳本語言編寫,例如瀏覽器可以理解的JavaScript或VBScript。
必須從控制項的ServerValidate事件處理程序調用伺服器端驗證例程。伺服器端驗證例程應使用任何.Net語言編寫,如C#或VB.Net。
控制項的基本語法如下所示:
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction=.cvf_func. ErrorMessage="CustomValidator"> </asp:CustomValidator>
ValidationSummary
ValidationSummary控制項不執行任何驗證,但顯示頁面中所有錯誤的摘要。摘要顯示驗證失敗的所有驗證控制項的ErrorMessage屬性的值。
以下兩個相互包含的屬性列出了錯誤消息:
ShowSummary:以指定格式顯示錯誤消息。
ShowMessageBox:在單獨的窗口中顯示錯誤消息。
控制項的語法如下所示:
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode = "BulletList" ShowSummary = "true" HeaderText="Errors:" />
Validation Groups
複雜的頁面在不同的面板中提供不同的信息組。在這種情況下,可能需要爲單獨的組單獨執行驗證。這種情況是使用驗證組處理的。
要創建驗證組,應通過設置輸入控制項和驗證控制項的validation group屬性,將它們放入同一邏輯組中。
Example
下面的例子描述了一個由一所學校的所有學生填寫的表格,分爲四個部分,用於選舉校長。在這裡,我們使用驗證控制項來驗證用戶輸入。
這是「設計」視圖中的表單:
內容文件代碼如下:
<form id="form1" runat="server"> <table style="width: 66%;"> <tr> <td class="style1" colspan="3" align="center"> <asp:Label ID="lblmsg" Text="President Election Form : Choose your president" runat="server" /> </td> </tr> <tr> <td class="style3"> Candidate: </td> <td class="style2"> <asp:DropDownList ID="ddlcandidate" runat="server" style="width:239px"> <asp:ListItem>Please Choose a Candidate</asp:ListItem> <asp:ListItem>M H Kabir</asp:ListItem> <asp:ListItem>Steve Taylor</asp:ListItem> <asp:ListItem>John Abraham</asp:ListItem> <asp:ListItem>Venus Williams</asp:ListItem> </asp:DropDownList> </td> <td> <asp:RequiredFieldValidator ID="rfvcandidate" runat="server" ControlToValidate ="ddlcandidate" ErrorMessage="Please choose a candidate" InitialValue="Please choose a candidate"> </asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style3"> House: </td> <td class="style2"> <asp:RadioButtonList ID="rblhouse" runat="server" RepeatLayout="Flow"> <asp:ListItem>Red</asp:ListItem> <asp:ListItem>Blue</asp:ListItem> <asp:ListItem>Yellow</asp:ListItem> <asp:ListItem>Green</asp:ListItem> </asp:RadioButtonList> </td> <td> <asp:RequiredFieldValidator ID="rfvhouse" runat="server" ControlToValidate="rblhouse" ErrorMessage="Enter your house name" > </asp:RequiredFieldValidator> <br /> </td> </tr> <tr> <td class="style3"> Class: </td> <td class="style2"> <asp:TextBox ID="txtclass" runat="server"></asp:TextBox> </td> <td> <asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass" ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" MinimumValue="6" Type="Integer"> </asp:RangeValidator> </td> </tr> <tr> <td class="style3"> Email: </td> <td class="style2"> <asp:TextBox ID="txtemail" runat="server" style="width:250px"> </asp:TextBox> </td> <td> <asp:RegularExpressionValidator ID="remail" runat="server" ControlToValidate="txtemail" ErrorMessage="Enter your email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator> </td> </tr> <tr> <td class="style3" align="center" colspan="3"> <asp:Button ID="btnsubmit" runat="server" onclick="btnsubmit_Click" style="text-align: center" Text="Submit" style="width:140px" /> </td> </tr> </table> <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode ="BulletList" ShowSummary ="true" HeaderText="Errors:" /> </form>
提交按鈕後面的代碼: