验证控件主要有以下6种:
1<asp:RequiredFieldValidator>
2<asp:RangeValidator>
3<asp:CompareValidator>
4 <asp:RegularExpressionValidator>
5 <asp:CustomValidator>//此控件最灵活
6<asp:ValidationSummary>
注:验证可以在服务端,客户端以及两者都可以进行验证,关键是使用时机.
具体用法见例:
//用于客户端验证一个数是否是素数
<script language="JavaScript">
<!--
// client-side validation function for CustomValidator
function ClientValidate(objSource, objArgs) {
var blnValid = true;
var intNumber = objArgs.Value;
if (intNumber % 2 == 1) {
var intDivisor = Math.floor(intNumber / 3);
if (intDivisor > 2) {
for (var i = 3; i <= intDivisor; i = i + 2) {
if (intNumber % intDivisor == 0) {
blnValid = false;
break;
}
}
}
else
blnValid = false;
}
else
blnValid = false;
objArgs.IsValid = blnValid;
return;
}
//-->
</script>
<form runat="server">
<table border="0">
<--asp:RequiredFieldValidator控件,用于验证必添项目-->
<tr>
<td align="right">A Required Value:</td>
<td><input type="text" id="txtRequired" size="20" runat="server" />
<asp:RequiredFieldValidator id="valRequired" runat="server"
ControlToValidate="txtRequired"
ErrorMessage="* You must enter a value in the first text box"
Display="dynamic">
*
</asp:RequiredFieldValidator>
</td>
</tr><tr>
<--asp:CompareValidator控件,用于比较,可以是与其它控件比较,也可以是与某个值比较-->
<td align="right">The Same Value Again:</td>
<td><input type="text" id="txtCompare" size="20" runat="server" />
<asp:CompareValidator id="valCompare" runat="server"
ControlToValidate="txtCompare"
ControlToCompare="txtRequired"
Operator="Equal"
ErrorMessage="* You must enter the same value in the second text box"
Display="dynamic">
*
</asp:CompareValidator>
</td>
</tr><tr>
<td align="right">A Date after 3rd March 2001:</td>
<td><input type="text" id="txtCompareDate" size="10" runat="server" />
<span class="cite">Tip: use something like "10/10/02"</span>
<asp:CompareValidator id="valCompareDate" runat="server"
ControlToValidate="txtCompareDate"
ValueToCompare="10/10/01"
Operator="GreaterThan"
Type="Date"
ErrorMessage="* The Date must be later than 3rd March 2001"
Display="dynamic">
*
</asp:CompareValidator>
</td>
</tr><tr>
<--asp:RangeValidator,验证是否在范围内-->
<td align="right">A Number between 1 and 10:</td>
<td><input type="text" id="txtRange" size="5" runat="server" />
<asp:RangeValidator id="valRange" runat="server"
ControlToValidate="txtRange"
MaximumValue="10"
MinimumValue="1"
Type="Integer" //比较类型
ErrorMessage="* The Number must between 1 and 10"
Display="dynamic">
*
</asp:RangeValidator>
</td>
</tr><tr>
<--asp:RegularExpressionValidator验证是否符合正规表达式-->
<td align="right">Match Expression "<b>.*@.*\..*</b>":</td>
<td><input type="text" id="txtRegExpr" size="40" runat="server" />
<span class="cite">Tip: enter a valid email address</span>
<asp:RegularExpressionValidator id="valRegExpr" runat="server"
ControlToValidate="txtRegExpr"
ValidationExpression=".*@.*\..*"
ErrorMessage="* Your entry does not match the regular expression"
Display="dynamic">
*
</asp:RegularExpressionValidator>
</td>
</tr><tr>
<--asp:CustomValidator自定义控件-->
<td align="right">A Prime Number over 100:</td>
<td><input type="text" id="txtCustom" size="5" runat="server" />
<span class="cite">Tip: 197 will work</span>
<asp:CompareValidator id="valComparePrime" runat="server"
ControlToValidate="txtCustom"
ValueToCompare="100"
Operator="GreaterThan"
Type="Integer"
ErrorMessage="* The Prime Number must be greater than 100"
Display="dynamic">
*
</asp:CompareValidator>
<asp:CustomValidator id="valCustom" runat="server"
ControlToValidate="txtCustom"
ClientValidationFunction="ClientValidate"//客户端验证
OnServerValidate="ServerValidate"//服务端验证
ErrorMessage="* Your knowledge of prime numbers is less than optimal"
Display="dynamic">
*
</asp:CustomValidator>
</td>
</tr>
</table>
<--asp:ValidationSummary 用于显示验证失败信息-->
<asp:ValidationSummary id="valSummary" runat="server"
HeaderText="<b>The following errors were found:</b>"
ShowSummary="true" DisplayMode="List" />
<hr />
Validation Enabled:
<asp:DropDownList AutoPostBack="True" id="lstEnabled" runat="server">
<asp:ListItem Text="True" /><asp:ListItem Text="False" /></asp:DropDownList>
EnableClientScript:
<asp:DropDownList AutoPostBack="True" id="lstClientScript" runat="server">
<asp:ListItem Text="True" /><asp:ListItem Text="False" /></asp:DropDownList>
ShowMessageBox:
<asp:DropDownList AutoPostBack="True" id="lstMessageBox" runat="server">
<asp:ListItem Text="False" /><asp:ListItem Text="True" /></asp:DropDownList>
<p />
<input type="submit" id="cmdConfirm" value="Submit" runat="server" onserverclick="ConfirmEntry">
<input type="submit" id="cmdCancel" value="Cancel" runat="server" causesvalidation="false" onserverclick="CancelEntry">
</form>
<div id="outMessage" EnableViewState="False" runat="server" />
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
foreach (BaseValidator objValidator in Page.Validators)
{
objValidator.Enabled = Convert.ToBoolean(lstEnabled.SelectedItem.Text);
objValidator.EnableClientScript = Convert.ToBoolean(lstClientScript.SelectedItem.Text);
}
valSummary.ShowMessageBox = Convert.ToBoolean(lstMessageBox.SelectedItem.Text);
}
void ServerValidate(Object sender, ServerValidateEventArgs e)
{
Boolean blnValid = true;
try
{
int intNumber = Convert.ToInt32(e.Value);
// check that it's an odd number
if ((intNumber % 2) == 1)
{
// get the largest possible divisor
int intDivisor = (int)(intNumber / 3);
if (intDivisor > 2)
{
// check using each divisor in turn
for (int intLoop = 3; intLoop <= intDivisor; intLoop += 2)
{
if ((intNumber % intDivisor) == 0)
{
blnValid = false;
break;
}
}
}
else
blnValid = false;
}
else
blnValid = false;
}
catch (Exception objError)
{
blnValid = false;
}
finally
{
e.IsValid = blnValid;
}
}
void ConfirmEntry(Object sender, EventArgs e)
{
outMessage.InnerHtml = "Page.IsValid returned <b>" + Page.IsValid + ".</b>";
}
void CancelEntry(Object sender, EventArgs e)
{
outMessage.InnerHtml = "Validation was not carried out.";
}
</script>