使用jquery.validate组件进行前端数据验证并实现异步提交前验证检查
学习如鹏网掌上组的项目开发,使用到了前端验证,视频里使用的ValidateForm验证框架,但是我使用的Hui的框架中使用的是jquery.validate验证框架
所以自行学习jquery.validate的使用 但是遇到了一个问题,就是我没有使用submit按钮进行提交,而是在button的点击事件中执行异步提交,这里就有一个问题就是要在提交之前先验证数据正确性再进行提交
最终查询官网文档后找到了方法https://jqueryvalidation.org/Validator.form/
validate()方法返回一个validator对象,其中form()方法就是用来验证表单的。如果通过验证规则,则返回true,否则返回false

下面上代码:我的验证规则是写在js代码中的
<form class="form form-horizontal" id="addForm">
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>手机号:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" value="" placeholder="" id="phonenum" name="phonenum">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>姓名:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" value="" placeholder="" id="name" name="name" >
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>密码:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="password" class="input-text" value="" placeholder="" id="password" name="password" >
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>确认密码:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="password" class="input-text" value="" placeholder="" id="confirmPassword" name="confirmPassword">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>邮箱:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" value="" placeholder="" id="email" name="email">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>城市:</label>
<div class="formControls col-xs-8 col-sm-9">
<select class="dropDown" id="city" name="cityId" required>
@foreach (var city in Model.Citys)
{
<option value="@city.Id">@city.Name</option>
}
</select>
</div>
</div> <div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色:</label>
@foreach (var role in Model.Roles)
{
<div class="check-box col-xs-4 col-sm-3">
<input type="checkbox" value="@role.Id" id="RoleIds_@role.Id" name="RoleIds">
<label for="RoleIds_@role.Id">@role.Name</label>
</div>
} </div>
<div class="row cl">
<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
<input class="btn btn-primary radius" type="button" id="saveBtn" value=" 提交 ">
</div>
</div>
</form>
下面是js:
function formValidate() {
var result = $("#addForm").validate({
rules: {
phonenum: {
required: true,
minlength: 1,
maxlength:50
},
name: {
required: true,
minlength: 1,
maxlength: 50
},
password: {
required: true,
minlength: 1,
maxlength: 50
},
confirmPassword: {
required: true,
minlength: 1,
maxlength: 50,
equalTo:"#password"
},
email: {
required: true,
email:true
}
},
onsubmit: true,
onfocusout: function (element) { $(element).valid(); },
onkeyup: function (element) { $(element).valid(); }
});
return result;
}
$(function () {
formValidate();
$("#saveBtn").click(function () {
if (formValidate().form()) {
var formData = $("#addForm").serializeArray();
$.ajax({
url: "/AdminUser/Add",
type: "post",
data: formData,
dataType: "json",
success: function (res) {
if (res.status == "ok") {
parent.location.reload();
} else {
alert("error");
}
},
error: function (res) {
alert("内部错误");
}
});
}
});
});
使用jquery.validate组件进行前端数据验证并实现异步提交前验证检查的更多相关文章
- javascprit form表单提交前验证以及ajax返回json
1.今天要做一个手机验证码验证的功能.需求是前端页面点击发送 短信验证码,后台接收后通过ajax返回到前端,之后前端在提交时候进行验证.思路很简单,不过做的过程还是学到不少的东西. 1.ajax请求后 ...
- 使用jQuery.form插件,实现完美的表单异步提交
传送门:异步编程系列目录…… 时间真快,转眼一个月快结束了,一个月没写博客了!手开始生了,怎么开始呢…… 示例下载:使用jQuery.form插件,实现完美的表单异步提交.rar 月份的尾巴,今天的主 ...
- MVC笔记2:mvc+jquery.validate.js 进行前端验证
1.引用如下js和css 代码 <link href="@Url.Content("~/Content/Site.css")" rel="sty ...
- jQuery Validate验证框架详解,提交前验证
现在都用h5表单进行验证了,以下方式仅做回忆 https://www.runoob.com/jquery/jquery-plugin-validate.html <!DOCTYPE HTML P ...
- jQuery validate在没有校验通过的情况下拒绝提交
下面通过一个简单的例子说明,这个问题,可能是很多人遇到的,验证不通过的时候,依然提交了表单. HTML <form class="survey" id="surve ...
- jquery.form.js 使用以及问题(表单异步提交)
标注:我引用的js后报错 原因:是引用的js有冲突 我引用了两便jQuery: 转载:https://blog.csdn.net/cplvfx/article/details/80455485 使用方 ...
- Html form 表单提交前验证
可以使用form表单的onsubmit方法,在提交表单之前,对表单或者网页中的数据进行检验. onsubmit指定的方法返回true,则提交数据:返回false不提交数据. 直接看下面的代码: 1 & ...
- a 标签提交前验证
最近在做验证的时候遇到了submit()与onsubmit()事件冲突的问题,本来想在a标签中添加submit()进行表单的提交,然后在 form中添加onsubmit事件触发验证方法.结果行不通,最 ...
- HTML:<input type="text"> 输入数字时的验证!(在提交时验证)
<!--非负数:<input type="text" name="" pattern="^\d+$">--> < ...
随机推荐
- linux100day(day5)--编程原理和shell脚本
通过前面的学习,我们对于linux文件系统有了一定的了解,我们接下来会初步接触编程原理和尝试编写shell脚本来实现功能. day05--编程原理和shell脚本初步认识 编程原理 在早期编程中,因为 ...
- mesos,marathon,haproxy on centos7 最完美安装教程
前言 本教程参考 http://blog.51cto.com/11863547/1903532 http://blog.51cto.com/11863547/1903532 官方文档等... 系统:c ...
- django项目中账号注册登陆使用JWT的记录
需求分析 1. 注册用JWT做状态保持 1.1 安装jwt pip install djangorestframework-jwt 1.2 去settings里面配置jwt ...
- java api 调用es集群(1.7版本)
public static void main(String[] args) { Settings settings = ImmutableSettings.settingsBuilder() // ...
- js 弹窗并定时关闭
1. $('input').click(function() { prompt('点击成功', 2000) }) function prompt(newName, time, fn) { var $d ...
- 0-3为变长序列建模modeling variable length sequences
在本节中,我们会讨论序列的长度是变化的,也是一个变量 we would like the length of sequence,n,to alse be a random variable 一个简单的 ...
- Apache Flink 进阶(六):Flink 作业执行深度解析
本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink Contributor.网易云音乐实时计算平台研发工程师岳猛分享.主要分享内容为 Flink Job 执行作业的 ...
- 4412 chmod权限
chmod权限 使用命令"man 2 chmod"学习chmod函数• int chmod(const char *path, mode_t mode);– 参数*path:文件路 ...
- HDU 6121 Build a tree —— 2017 Multi-University Training 7
HazelFan wants to build a rooted tree. The tree has nn nodes labeled 0 to n−1, and the father of the ...
- VC++ 控件赋值取值
SetWindowText(SetWindowTextW)void SetWindowText( LPCTSTR lpszString );GetWindowText(GetWindowTextW ...