在ASP.NET 3.5中使用自定义验证器 [英] Using Custom Validator in ASP.NET 3.5
本文介绍了在ASP.NET 3.5中使用自定义验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在尝试使用自定义验证程序来验证用户名的长度.我正在使用VBScript作为脚本语言.
但是,当我尝试运行代码时,出现错误.
以下是我在ASP.NET页面中使用的脚本.
Hello All,
I am trying to use a Custom Validator for the validation of the Length of the User Name. I am using VBScript as the scripting language.
But when I try to run the code i am getting an Error.
Below is the script which I am using in m ASP.NET page.
<script language="vbscript" type="text/vbscript">
Sub Validate (val,arguments)
If(val.Length=6) Then
arguments.IsValid=True
Else
arguments.IsValid=False
End If
End Sub
</script>
以下是CustomValidtor的代码.
Following is the code for the CustomValidtor.
<asp:customvalidator id="CustomValidator1" runat="server" xmlns:asp="#unknown">
ErrorMessage="User name length should be minimum 6 characters"
onservervalidate="CustomValidator1_ServerValidate"
ClientValidationFunction="Validate" ControlToValidate="TextBox1"></asp:customvalidator>
请协助.
非常感谢Advance
Kindly assist.
Many Thanks in Advance
推荐答案
我希望我知道VBScript,但是我不知道.但是,我能够使用Javascript正确运行自定义验证".请尝试将JavaScript代码修改为VBScript,我相信这对您来说并不困难:)
这是代码:
(请注意,我还添加了RequiredFieldValidator来验证空输入)
I wish I knew VBScript, but, I don''t know it. However, I was able to run the Custom Validation properly using Javascript. Please try to modify the JavaScript code to VBScript, I believe it won''t be hard for you :)
Here is the Code:
(Please note that, I also added a RequiredFieldValidator to validate the empty inputs)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function Validate(sender, args)
{
if(args.Value.length == 6)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CustomValidator id="CustomValidator1" Display="Dynamic" runat="server"
ErrorMessage="User name length should be minimum 6 characters"
onservervalidate="CustomValidator1_ServerValidate"
ClientValidationFunction="Validate" EnableClientScript="true" ControlToValidate="TextBox1"/><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" ErrorMessage="User name required" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
这篇关于在ASP.NET 3.5中使用自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文