<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery-1.9.1.min.js"></script>
<script src="jquery.validate.min.js"></script>
<script src="messages_zh.min.js"></script>
<script src="jquery.form.js"></script>
<style>
.error{
color:red;
}
</style>
</head>
<body>
<form action="index.php" id="myform">
<p>
<input type="text" name="userName">
</p>
<p>
<input type="text" name="age">
</p>
<input type="submit">
</form>
</body>
<script>
var a="请确保以字母开头"
$.validator.addMethod("letterfirst",function(value,el){
value=value.toLowerCase();
var patt=/^[a-z]/;
return this.optional(el)||patt.test(value);
},a)
$("#myform").validate({
rules:{
userName:{
required:true,
letterfirst:true,
remote:{
url:"index.php"
}
},
age:"required"
},
messages:{
userName:{
remote:"用户名已存在"
}
},
errorPlacement:function(error,el){
el.css({border:"2px solid red"})
error.appendTo(el.parent());
},
success:function(error){
error.siblings('input').css({borderColor:"green"});
error.remove();
},
submitHandler:function(form){
// form.submit();
$(form).ajaxSubmit({
success:function(res){
alert(res,"aaaaaaaaaaaaaaaaaaaa")
}
})
},
debug:true
})
</script>
</html>
网友评论