我試過使用jquery驗證插件進行基本的正則表達式驗證,但事情是它只適用於規則'required',但不適用於add方法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>
你期望你的正則表達式做什麼?換句話說,你試圖創建的規則應該是什麼樣的測試? – Sparky