⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asp_validators.aspx

📁 东软内部材料(四)asp等相关的教学案例 
💻 ASPX
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

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

<!-- #include file="style.inc" -->
</head>
<body bgcolor="#ffffff">
<!--------------------------------------------------------------------------->
<span class="heading">ASP.NET Validation Controls</span> &nbsp; &nbsp; &nbsp;

<form runat="server">
 <table border="0">
 <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>
  <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="3/3/2001"
           Operator="GreaterThan"
           Type="Date"
           ErrorMessage="* The Date must be later than 3rd March 2001"
           Display="dynamic">
           *
      </asp:CompareValidator>
  </td>
 </tr><tr>
  <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>
  <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>
  <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 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>
&nbsp; &nbsp;EnableClientScript:
<asp:DropDownList AutoPostBack="True" id="lstClientScript" runat="server">
<asp:ListItem Text="True" /><asp:ListItem Text="False" /></asp:DropDownList>
&nbsp; &nbsp;ShowMessageBox:
<asp:DropDownList AutoPostBack="True" id="lstMessageBox" runat="server">
<asp:ListItem Text="False" /><asp:ListItem Text="True" /></asp:DropDownList>
<p />&nbsp; &nbsp;
<input type="submit" id="cmdConfirm" value="Submit" runat="server" onserverclick="ConfirmEntry">&nbsp; &nbsp;
<input type="submit" id="cmdCancel" value="Cancel" runat="server" causesvalidation="false" onserverclick="CancelEntry">&nbsp; &nbsp;
</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>


<!--------------------------------------------------------------------------->
<!-- #include file="foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -