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

直接贴样式代码

<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. 前端必备基础知识之--------原生JS发送Ajax请求

    原生JS发送Ajax请求 ajax({ type: 'POST', url: 'http://10.110.120.123:2222', // data: param, contentType: 'a ...

  2. 【学习笔记】动态树 Link-Cut Tree

    - 闲话 LCT 优秀博客: \(\color{black}{\textsf{F}}\color{red}{\textsf{lashHu}}\) 大佬的 cnblogs:https://www.cnb ...

  3. render到底是什么,该如何使用它

    一.前言 1.vue程序的运行过程:模板 -> 进行编译 -> 生成ast树 -> 数据绑定 -> 生成render函数 -> 成虚拟dom树 -> 真实dom树 ...

  4. 静态static关键字概述-静态static关键字修饰成员变量

    静态static关键字概述 概述 关于 static 关键字的使用,它可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单是属 于某个对象的.也就是说,既然属于类,就可以不靠创建对象来 ...

  5. 【Android】Android 源码方式使用 opencv 库文件

    使用方法 opencv 官方的 SDK 已经有编译好的 so 库,我们可以直接使用,因此我们只需要将平台架构对应的 so 库文件以及头文件提取出来,使用 Android.mk 进行预安装,然后在需要使 ...

  6. python生成自动化测试报告并发送到指定邮箱

    #-*-coding:utf-8 -*- import HTMLTestRunner import unittest import time import sys import os import s ...

  7. wsl 网络探究

    省流:wsl2能否固定ip地址? - 豆腐干的回答 - 知乎 https://www.zhihu.com/question/387747506/answer/2764445888 割--------- ...

  8. Nginx09 http的keepalive及在nginx的配置使用

    1 为什么要有Connection: keep-alive? 在早期的HTTP/1.0中,每次http请求都要创建一个连接,而创建连接的过程需要消耗资源和时间,为了减少资源消耗,缩短响应时间,就需要重 ...

  9. SQL笔试真题练习

    一.现有以下两张表: 第一张表名为cust,其表结构如下: 字段名 字段说明 是否主键 Studentno 学号,数据类型为整型的 是 Name 学生名字,数据类型为字符串型的 否 Address 学 ...

  10. 安装和配置Maven项目管理工具

    1.下载Maven工具包:https://maven.apache.org/download.cgi 2.解压apache-maven-3.8.4-bin.zip 3.修改apache-maven-3 ...