[英]CustomValidator with js function doesn`t work
i created CustomValidator with js function in wizard view just to check password length. 我在向导视图中使用js函数创建了CustomValidator,只是为了检查密码长度。 And he dosn`t works, sorry for my newbieness.
而且他不工作,对不起我了。
js function: js函数:
<script type="text/javascript">
function clientValidate(sender, args) {
if (args.Value.length < 5 ||args.Value.length > 30) {
args.IsValid = false;
}
}
</script>
asp fields: asp栏位:
<asp:TextBox ID="tbPassword1" runat="server" TextMode="Password"></asp:TextBox>
<asp:CustomValidator ID="CustomValidatorPassLength" runat="server" display="Dynamic" ControlToValidate="tbPassword1" ClientValidationFunction="clientValidate" ErrorMessage="Slaptažodis turi būti nuo 5 iki 30 simbolių ilgio." ForeColor="Red"></asp:CustomValidator>
Thanks and sorry! 谢谢,抱歉!
The ASP Custom Validator will fire only when the user attempts to submit the webform via a button click, etc. If you want to run the validation during a client-side event, you'll have to use Javascript, not an ASP server control. 仅当用户尝试通过单击按钮等方式提交Web表单时,ASP Custom Validator才会触发。如果要在客户端事件期间运行验证,则必须使用Javascript,而不是ASP服务器控件。
See this JS Fiddle Demo for an example of what you could do. 请参阅此JS Fiddle演示 ,以获取有关您可以做什么的示例。 The code is below.
代码如下。
<input type="text" class="password" id="mytextbox"></input><span class="errormsg">Password length invalid</span>
$(function(){
// This is to add the "On Change" event to your textbox.
$('#mytextbox').change(function(){
if(!validateFunction($(this))){
$(this).next('.errormsg').fadeIn({duration: 400});
} else {
$(this).next('.errormsg').fadeOut({duration: 100});
}
});
})
// This is the function that validates your checkbox. It returns true or false.
function validateFunction(obj){
if(obj.hasClass('password')){
var txt = obj.val();
return (txt.length > 5 && txt.length <= 30);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.