如下图,当执行提交操作之前,我们需要对序号,要求完成时间,责任人,措施内容四项进行非空,字符长度及输入内容的类型进行校验.

直接贴样式代码

<div class="wrapper animated fadeInRight">
<form id="form" class="form-horizontal m">
<div class="form-group">
<label class="col-sm-3 control-label ">序号<font class="red"> *</font></label>
<div class="col-sm-8">
<input id="longEventId" col="LongEventId" type="hidden" class="form-control" />
<input id="stepNo" name="stepNoName" col="StepNo" type="number" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label ">要求完成时间<font class="red"> *</font></label>
<div class="col-sm-8">
<input id="finishTime" name="finishTime" col="FinishTime" type="text" class="time-input form-control"
autocomplete="off" placeholder="要求完成时间" />
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label ">责任人<font class="red"> *</font></label> @*<input id="personLiableId" col="PersonLiableId" type="text" class="form-control" />*@
@await Html.PartialAsync("/Areas/SystemManage/Shared/SystemUserIdSelect.cshtml",
new ViewDataDictionary(this.ViewData) { { "Content", "8" }, { "IsMultiple", "false" } })
<input id="userId" name="userIdName" col="PersonLiableId" type="hidden" class="form-control" />
</div>
<div class="form-group">
<label class="col-sm-3 control-label ">改善对策</label>
<div class="col-sm-8">
<textarea id="basicReason" col="BasicReason" class="form-control" style="height:60px"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label ">措施内容<font class="red"> *</font></label>
<div class="col-sm-8">
<textarea id="stepContent" name="stepContentName" col="StepContent" class="form-control"
style="height:60px"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label ">图片</label>
<div class="col-sm-8">
<div id="stepPic" class="img-box"></div>
</div>
</div>
</form>
</div>

下面为文本输入检测代码

<script type="text/javascript">
$(function () {
laydate.render({
elem: '#finishTime',
type: 'datetime',
format: 'yyyy-MM-dd HH:mm:ss'
}); $('#form').validate({
rules: {
stepNoName: {required: true},
finishTime: {required: true},
userId:{required: true},
stepContentName: {required: true, maxlength: 300 }
}
});
}); function saveForm(index) {
var userI = $("#userId").val();
if (userI == "" || userI == null || userI == undefined) {
ys.msgError("请选择责任人!");
return;
} if ($('#form').validate().form()) {
var postData = $('#form').getWebControls({Id: id });
postData.StepPic = $("#stepPic").imageUpload("getImageUrl");
ys.ajax({
url: '@Url.Content("~/LongEventManage/LongMeasuresManage/SaveFormJson")',
type: 'post',
data: postData,
success: function (obj) {
if (obj.Tag == 1) {
ys.msgSuccess(obj.Message);
parent.searchGrid();
parent.layer.close(index);
}else {
ys.msgError(obj.Message);
}
}
});
}
} </script>

先看下当点击提交的时候的效果图:

当我们点击提交时,序号,要求完成时间,措施内容都提示是必填字段.

因此当提交时,这个表单验证是不会通过的,也就不会执行ajax请求调用提交方法.

if ($('#form').validate().form()) {
var postData = $('#form').getWebControls({Id: id });
postData.StepPic = $("#stepPic").imageUpload("getImageUrl");
ys.ajax({
url: '@Url.Content("~/LongEventManage/LongMeasuresManage/SaveFormJson")',
type: 'post',
data: postData,
success: function (obj) {
if (obj.Tag == 1) {
ys.msgSuccess(obj.Message);
parent.searchGrid();
parent.layer.close(index);
}else {
ys.msgError(obj.Message);
}
}
});
}
}

咱们接着往下实验,如果此时我们输入了措施内容,但是字符大于规定的300长度时,

stepContentName: {required: true, maxlength: 300 }

关于rules的key,其实指向的<input />中的name,之前我选择的是id,但是却没有生效,必须指向他的name名称.

细心的小伙伴,有没有发现,我的完成时间,为什么没有用name,而是还用的id名称?

<input id="stepNo" name="stepNoName" col="StepNo" type="number" class="form-control" />
<input id="finishTime" name="finishTimeName" col="FinishTime" type="text" class="time-input form-control" autocomplete="off" placeholder="要求完成时间" />
 $('#form').validate({
rules: {
stepNoName: {required: true},
finishTime: {required: true},
userId:{required: true},
stepContentName: {required: true, maxlength: 300 }
}
});
});

因为上面的<script>代码中,我使用了layUI框架对时间选择器的修饰导致name没有生效.
laydate.render({
elem: '#finishTime',
type: 'datetime',
format: 'yyyy-MM-dd HH:mm:ss'
});

其实我们从F12也可以看出,完成时间input框他的name也是finishTime.所以还是用的name作为Key.
 
 

JS form表单数据校验及失效情况下的解决方案的更多相关文章

  1. js form表单的校验

    if(!$("#form").validate().form()){ return false;} <元素 class="required">< ...

  2. 表单数据验证方法(一)—— 使用validate.js实现表单数据验证

    摘要:使用validate.js在前端实现表单数据提交前的验证 好久没写博客了,真的是罪过,以后不能这样了,只学习不思考,学的都是白搭,希望在博客园能记录下自己学习的点滴,虽然记录的都是些浅显的技术, ...

  3. SpringBoot表单数据校验

    Springboot中使用了Hibernate-validate作为默认表单数据校验框架 在实体类上的具体字段添加注解 public class User { @NotBlank private St ...

  4. FastAPI框架入门 基本使用, 模版渲染, form表单数据交互, 上传文件, 静态文件配置

    安装 pip install fastapi[all] pip install unicorn 基本使用(不能同时支持,get, post方法等要分开写) from fastapi import Fa ...

  5. [JavaScript] 实现简单的表单数据校验功能

    实现表单数据校验功能 因为项目用的UI库功能太少,表单不具备校验功能,所以自己写了一个,只有一个文件. 使用 import { required, email, useValidate } from ...

  6. mysql在生产环境下有大量锁表,又不允许重启的情况下的处理办法

    mysql在生产环境下有大量锁表,又不允许重启的情况下的处理办法 满头大汗的宅鸟该怎么办呢? mysql -u root -e "show processlist"|grep -i ...

  7. JS form表单提交的方法

    1.当输入用户名和密码为空的时候,需要判断.这时候就用到了校验用户名和密码,这个需要在jsp的前端页面写:有两种方法,一种是用submit提交.一种是用button提交.方法一: 在jsp的前端页面的 ...

  8. JS form 表单收集 数据 formSerialize

    做后台系统的时候通常会用到form表单来做数据采集:每次一个字段一个字段的去收集就会很麻烦,网站也有form.js插件可以进行表单收集,并封装成一个对象,通过ajax方法传到后台:现在介绍一种直觉采集 ...

  9. js form 表单属性学习

    一.<form></form>标签      引用借鉴:http://www.cnblogs.com/fizx/p/6703370.html form标签的属性规定了当前网页上 ...

  10. js——form表单验证

    用js实现一个简易的表单验证 效果: 代码: <html> <head> <title>js校验form表单</title> <meta char ...

随机推荐

  1. 在函数中设置input的multiple属性以及input的点击事件时,设置失效

    1.在函数中先设置input文件可以多选,然后再设置input框的点击事件情况下,有时候这个多选设置会失效. 我们可以采用下面的方式去解决 <input ref="myInputRef ...

  2. 绝对路径和相对路径-file类的构造方法

    绝对路径和相对路径 路径:绝对路径:是一个完整的路径以盘符(c:,D:)开始的路径c:lla.txtC; llUserslitcastllIdeaProjects 1lshungyuan {\123. ...

  3. Java入门与进阶P-3.7+P-3.8

    猜数游戏 让计算机来想一个数,然后让用户来猜,用户每输入一个数,就告诉它这是大了还是小了,知道用户猜中为止,最后还要告诉用户它猜了多少次 因为需要不断重复让用户猜,所以需要用到循环 在实际写出程序之前 ...

  4. CSS-@规则(At-rules)常用语法使用总结

    At-rules规则是目前CSS中一种常见的语法规则,它使用一个"@"符号加一个关键词定义,后面跟上语法区块,如果没有则以分号结束即可. 这种规则一般用于标识文档.引入外部样式.条 ...

  5. day04-视图和视图解析器

    视图和视图解析器 1.基本介绍 在SpringMVC中的目标方法,最终返回的都是一个视图(有各种视图) 注意,这里的视图是一个类对象,不是一个页面!! 返回的视图都会由一个视图解析器来处理(视图解析器 ...

  6. Redis 异步客户端选型及落地实践

    作者:京东科技 王晨 Redis异步客户端选型及落地实践 可视化服务编排系统是能够通过线上可视化拖拽.配置的方式完成对接口的编排,可在线完成服务的调试.测试,实现业务需求的交付,详细内容可参考:htt ...

  7. 花了半个小时基于 ChatGPT 搭建了一个微信机器人

    相信大家最近被 ChatGPT 刷屏了,其实在差不多一个月前就火过一次,不会那会好像只在程序员的圈子里面火起来了,并没有被大众认知到,不知道最近是因为什么又火起来了,而且这次搞的人尽皆知. 想着这么火 ...

  8. 快速上手Java开发工具Eclipse之简易手册

    Eclipse下载,可以下载最新版本,文档是以2020-12R版本为例 http://www.eclipse.org/downloads/ 下载Packages即可 安装Eclipse 解压安装 除了 ...

  9. python3使用requests模块发https请求,提示caused by ssl error, can't connect to https url because the ssl module is not available

    Q: python3使用requests模块发https请求,提示caused by ssl error, can't connect to https url because the ssl mod ...

  10. ctfshow_web入门 文件上传

    文件上传 还是是一边学习一边做笔记的文章,由于是学习,所以会显得啰嗦 还请各位师傅担待~ 如果有不正确的地方,请师傅们斧正谢谢师傅们 web150 火狐关闭js的插件一旦开启,就没法点击上传按钮了. ...