我的问题是,当我修改密码值时,我有一个输入密码有一个custumValidator我检查密码是否包含名称。当我编辑字段名称时,我需要触发相同的customvalidator(客户端验证): 我怎么能这样做:
<form id="Form1" runat="server">
<h3>CustomValidator ServerValidate Example</h3>
<asp:Label id="Message"
Text="Enter an even number:"
Font-Name="Verdana"
Font-Size="10pt"
runat="server"/>
<p>
<asp:TextBox id="name"
runat="server" />
<asp:TextBox id="password"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="password"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Name="verdana"
Font-Size="10pt"
runat="server"/>
<p>
<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>
</form>
</body>
</html>
<script language="javascript">
function ClientValidate(source, arguments)
{
// here i will implement :if source contains name
arguments.IsValid = true;
else {
arguments.IsValid = false;
}
}
</script>
答案 0 :(得分:0)
尝试添加此内容:
修改强>:
<form id="Form1" runat="server">
<script type="text/javascript">
function ValidatePassword (source, arguments){
// Gets the name field
var nameField = document.getElementById('<%= name.ClientId %>');
// using indexOf since contains does not exist in js.
// If it is not found it will return -1
arguments.IsValid = (arguments.Value.indexOf(nameField.value) == -1);
}
</script>
<asp:TextBox id="name"
runat="server" />
<asp:TextBox id="password"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="password"
ClientValidationFunction="ValidatePassword"
ErrorMessage="Password should not contain your user name!"
runat="server"/>
<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>
</form>