jquery.validate.js只能对表单进行验证吗
2个回答
展开全部
验证的 规则需要自己写。验证的元素是自己定的 。
以下 是一个完整的 注册验证 C# 异步验证
$(document).ready(function ()
{
// 页面加载时调用
$(function ()
{
$('#registerform').validate({
debug: true,
rules: {
txtEmail: {
required: true,
email: true,
maxlength: 40,
remote: {
url: "../common/registandlogin.ashx?cmd=email", //后台处理程序
type: "get", //数据发送方式
dataType: "json", //接受数据格式
data: {//要传递的数据
emailcode: function ()
{
return $("#txtEmail").val();
}
}
}
},
txtUserPwd: {
required: true,
maxlength: 18,
minlength: 6
},
txtRUserPwd: {
required: true,
equalTo: "#txtUserPwd"
},
txtPhone: {
required: true,
minlength: 7,
maxlength: 13,
isMobile: true
},
txtCheck: "required",
captcha: {
required: true,
remote: {
url: "../common/registandlogin.ashx?cmd=captcha", //后台处理程序
type: "get", //数据发送方式
dataType: "json", //接受数据格式
data: {//要传递的数据
vcode: function ()
{
return $("#txtcaptcha").val();
}
}
}
}
},
messages: {
txtEmail: {
required: "邮箱不能为空!",
email: "邮箱格式不正确!",
maxlength: "最长40个字符!",
remote: "很遗憾,您输入的邮箱已被使用!"
},
txtUserPwd: {
required: "密码不能为空!",
maxlength: "最长为18个字符!",
minlength: "最短为6个字符!"
},
txtRUserPwd: {
required: "密码不能为空!",
equalTo: "两次输入密码不相同!"
},
txtPhone: {
required: "电话不能为空!",
minlength: "最少7个数字!",
maxlength: "最长13个数字!",
isPhone: "联系电话格式格式不正确!"
},
txtCheck: {
required: "请阅读用户协议!"
},
captcha: {
required: "请输入验证码!",
remote: "验证码输入错误!"
}
},
validClass: "sucess",
errorClass: "error",
errorElement: "p",
focusCleanup: true, //被验证的元素获得焦点时移除错误信息
errorPlacement: function (error, element)
{
error.appendTo(element.parent());
},
submitHandler: function (form)
{
var formData = $(form).serialize();
$.ajax({
cache: false,
type: "POST",
url: "../common/registandlogin.ashx?cmd=add&tk=" + (new Date()).getTime(),
data: formData,
error: function (request)
{
alert('链接失败.');
},
success: function (data)
{
if (data == "true")
{
location.href = 'memberlogin.aspx?email=' + $("#txtEmail").val();
} else
{
alert('注册失败!');
}
}
});
},
success: function (p)
{
p.html(" ").addClass("sucess");
}
});
//所有使用“.registerText”样式的文本框加上效果,获得焦点文本框变成淡黄色
$(".registerText").focus(function ()
{
$(this).css("background-color", "#FFFFCC").blur(function ()
{
$(this).css("background-color", "#FBFBFB");
});
});
$(".registerText_yzm").focus(function ()
{
$(this).css("background-color", "#FFFFCC").blur(function ()
{
$(this).css("background-color", "#FBFBFB");
});
});
});
});
以下 是一个完整的 注册验证 C# 异步验证
$(document).ready(function ()
{
// 页面加载时调用
$(function ()
{
$('#registerform').validate({
debug: true,
rules: {
txtEmail: {
required: true,
email: true,
maxlength: 40,
remote: {
url: "../common/registandlogin.ashx?cmd=email", //后台处理程序
type: "get", //数据发送方式
dataType: "json", //接受数据格式
data: {//要传递的数据
emailcode: function ()
{
return $("#txtEmail").val();
}
}
}
},
txtUserPwd: {
required: true,
maxlength: 18,
minlength: 6
},
txtRUserPwd: {
required: true,
equalTo: "#txtUserPwd"
},
txtPhone: {
required: true,
minlength: 7,
maxlength: 13,
isMobile: true
},
txtCheck: "required",
captcha: {
required: true,
remote: {
url: "../common/registandlogin.ashx?cmd=captcha", //后台处理程序
type: "get", //数据发送方式
dataType: "json", //接受数据格式
data: {//要传递的数据
vcode: function ()
{
return $("#txtcaptcha").val();
}
}
}
}
},
messages: {
txtEmail: {
required: "邮箱不能为空!",
email: "邮箱格式不正确!",
maxlength: "最长40个字符!",
remote: "很遗憾,您输入的邮箱已被使用!"
},
txtUserPwd: {
required: "密码不能为空!",
maxlength: "最长为18个字符!",
minlength: "最短为6个字符!"
},
txtRUserPwd: {
required: "密码不能为空!",
equalTo: "两次输入密码不相同!"
},
txtPhone: {
required: "电话不能为空!",
minlength: "最少7个数字!",
maxlength: "最长13个数字!",
isPhone: "联系电话格式格式不正确!"
},
txtCheck: {
required: "请阅读用户协议!"
},
captcha: {
required: "请输入验证码!",
remote: "验证码输入错误!"
}
},
validClass: "sucess",
errorClass: "error",
errorElement: "p",
focusCleanup: true, //被验证的元素获得焦点时移除错误信息
errorPlacement: function (error, element)
{
error.appendTo(element.parent());
},
submitHandler: function (form)
{
var formData = $(form).serialize();
$.ajax({
cache: false,
type: "POST",
url: "../common/registandlogin.ashx?cmd=add&tk=" + (new Date()).getTime(),
data: formData,
error: function (request)
{
alert('链接失败.');
},
success: function (data)
{
if (data == "true")
{
location.href = 'memberlogin.aspx?email=' + $("#txtEmail").val();
} else
{
alert('注册失败!');
}
}
});
},
success: function (p)
{
p.html(" ").addClass("sucess");
}
});
//所有使用“.registerText”样式的文本框加上效果,获得焦点文本框变成淡黄色
$(".registerText").focus(function ()
{
$(this).css("background-color", "#FFFFCC").blur(function ()
{
$(this).css("background-color", "#FBFBFB");
});
});
$(".registerText_yzm").focus(function ()
{
$(this).css("background-color", "#FFFFCC").blur(function ()
{
$(this).css("background-color", "#FBFBFB");
});
});
});
});
2016-03-11 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
是的,要写好校验规则。
1、将校验规则写到控件中
<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../js/jquery.validate.js" type="text/javascript"></script>
<script src="./js/jquery.metadata.js" type="text/javascript"></script>
$().ready(function() {
$("#signupForm").validate();
});
<form id="signupForm" method="get" action="">
<p>
<label for="firstname">Firstname</label>
<input id="firstname" name="firstname" class="required" />
</p>
<p>
<label for="email">E-Mail</label>
<input id="email" name="email" class="required email" />
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" type="password" class="{required:true,minlength:5}" />
</p>
<p>
<label for="confirm_password">确认密码</label>
<input id="confirm_password" name="confirm_password" type="password" class="{required:true,minlength:5,equalTo:'#password'}" />
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</form>
2.将校验规则写到js代码中
$().ready(function() {
$("#signupForm").validate({
rules: {
firstname: "required",
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
}
},
messages: {
firstname: "请输入姓名",
email: {
required: "请输入Email地址",
email: "请输入正确的email地址"
},
password: {
required: "请输入密码",
minlength: jQuery.format("密码不能小于{0}个字 符")
},
confirm_password: {
required: "请输入确认密码",
minlength: "确认密码不能小于5个字符",
equalTo: "两次输入密码不一致不一致"
}
}
});
});
1、将校验规则写到控件中
<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../js/jquery.validate.js" type="text/javascript"></script>
<script src="./js/jquery.metadata.js" type="text/javascript"></script>
$().ready(function() {
$("#signupForm").validate();
});
<form id="signupForm" method="get" action="">
<p>
<label for="firstname">Firstname</label>
<input id="firstname" name="firstname" class="required" />
</p>
<p>
<label for="email">E-Mail</label>
<input id="email" name="email" class="required email" />
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" type="password" class="{required:true,minlength:5}" />
</p>
<p>
<label for="confirm_password">确认密码</label>
<input id="confirm_password" name="confirm_password" type="password" class="{required:true,minlength:5,equalTo:'#password'}" />
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</form>
2.将校验规则写到js代码中
$().ready(function() {
$("#signupForm").validate({
rules: {
firstname: "required",
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
}
},
messages: {
firstname: "请输入姓名",
email: {
required: "请输入Email地址",
email: "请输入正确的email地址"
},
password: {
required: "请输入密码",
minlength: jQuery.format("密码不能小于{0}个字 符")
},
confirm_password: {
required: "请输入确认密码",
minlength: "确认密码不能小于5个字符",
equalTo: "两次输入密码不一致不一致"
}
}
});
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询