我已经尝试使用 jquery 验证插件进行基本的正则表达式验证,但问题是它仅适用于“必需”规则,但不适用于添加方法 regexvalid。如何实现这一目标
提前致谢,
<!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>
<title></title>
<script type="text/javascript" src="js/jquery-1.9.0.mini.js" > </script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.js" ></script>
<script type="text/javascript">
$(document).ready(function () {
jQuery.validator.addMethod("regexvalid",function(element,value){
return this.optional(element)|| /[0-9]*/.test(value);
});
$('#logform').validate({
rules: {
txtUsr: {
required: true,
regexvalid:true
},
txtPass: {
required: true
}
},
messages: {
txtUsr: {
required: 'Please enter username',
regexvalid:'valid numbers'
},
txtPass: {
required: 'Please enter password'
}
}
});
});
</script>
<style type="text/css">
.error
{
border-color:Red;
border-style:solid;
border-width:1px;
}
#signupForm label.error {
margin-left: 10px;
width: auto;
display: inline;
}
</style>
</head>
<body>
<div id='LoginDiv' style='width:400px;height:300px;background-color:green;'>
<form id='logform'>
User Name : <input type="text" id='txtUsr' name='txtUsr' style='margin-top:12px'/>
<br/>
<br/>
Password : <input type="text" id='txtPass' name='txtPass' />
<br/>
<br/>
<input type="submit" id='btnSubmit' value='Submit' />
</form>
</div>
</body>
</html>