我在向导视图中创建了带有js函数的CustomValidator,只是为了检查密码长度。他不工作,很抱歉我的新鲜感。
js职能:
<script type="text/javascript">
function clientValidate(sender, args) {
if (args.Value.length < 5 ||args.Value.length > 30) {
args.IsValid = false;
}
}
</script>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>谢谢,对不起!
发布于 2014-02-04 18:01:17
只有当用户试图通过单击按钮等方式提交网页时,ASP自定义验证器才会触发。如果您想在客户端事件期间运行验证,则必须使用Javascript,而不是ASP服务器控件。
有关您可以做什么的示例,请参阅此JS Fiddle演示。代码在下面。
HTML
<input type="text" class="password" id="mytextbox"></input><span class="errormsg">Password length invalid</span>JQuery / Javascript
$(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);
}
};https://stackoverflow.com/questions/21558720
复制相似问题