检测表单中的不能为空(.notnull)的验证
作用:一对form标签下有多个(包括一个)表单需要提交时,使用js准确的判断当前按钮对那些元素做判断
用法:在form标签下 找到当前 表单的容器 给予class="form",当前表单的提交按钮给予 class="check"
需要验证为空的元素给予class="notnull" nullmsg="xx不能为空!"提示,需要进行逻辑判断的表单给予class="need"
判断的类型给予 class="num"(只能是数字) 验证的提示 logicmsg="XX只能是数字"
给予class="errorMessage"显示错误信息块
给予class="warn"显示错误信息
未使用js面向对象编程
逻辑判断,不传入need标识,直接给出正则表达式属性(自定义)regex="/^\d$/" 做出判断
在外部实现
Global.submitCallback button回调函数
Global.confirmCallback confirm回调函数;
需要改进的地方:
暂无
///
*/
//$(document).ready(
// function () {
// $("form").find(".notnull").bind({
// focus: function () {
// if ($(this).attr("value") == this.defaultValue) {
// $(this).attr("value", "");
// }
// },
// blur: function () {
// if ($(this).attr("value") == "") {
// $(this).attr("value", this.defaultValue);
// }
// }
// });
// }
//);
///*封装一个万能检测表单的方法*/
///event.srcElement:引发事件的目标对象,常用于onclick事件。
///event.fromElement:引发事件的对象源,常用于onmouseout和onmouseover事件。
///event.toElement:引发事件后,鼠标移动到的目标源,常用于onmouseout和onmouseover事件。
function Global() {
var _self = this;
}
Global.submitCallback = null;
Global.confirmCallback = null;
$(document).ready(function () {
//form body
$("body").find(".form").each(function () {
this.onclick = function (e) {
var button = null;
try {
button = e.srcElement == null ? document.activeElement : e.srcElement;
} catch (e) {
console.log(e.message)
button = document.activeElement;
}
if ($(button).is(".check")) {
//alert("提交")
var sub = (checkform(this) && CheckInputRex(this) && checkselect(this) && checkChecked(this));
if (sub) {
// Call our callback, but using our own instance as the context
Global.submitCallback.call(this, [e]);
}
return sub;
} else if ($(button).is(".confirm")) {
//alert("删除")
var sub = confirm($(button).attr("title"));
if (sub) {
Global.confirmCallback.call(this, [e]);
}
return sub;
} else {
// //alert("其它")
return true;
}
}
});
/*检测表单中不能为空的元素*/
function checkform(form) {
var b = true;
$(form).find(".notnull").each(function () {
if ($.trim($(this).val()).length // if (this.value != null) {
// $(this).attr("value", "");
// }
//alert($(this).attr("msg"))
$(this).parents(".form").find(".warn").text($(this).attr("nullmsg"));
$(this).parents(".form").find(".errorMessage").show();
$(this).select();
$(this).focus();
return b = false;
}
});
if (b == true) {
$(form).find(".warn").text("");
$(form).find(".errorMessage").hide();
}
return b;
}
/*检测表单中必选的下拉列表*/
function checkselect(form) {
var b = true;
$(form).find(".select").each(function (i) {
var ck = $(this).find('option:selected').text();
if (ck.indexOf("选择") > -1) {
$(this).parents(".form").find(".warn").text($(this).attr("nullmsg"));
$(this).parents(".form").find(".errorMessage").show();
$(this).select();
$(this).focus();
return b = false;
}
});
return b;
}
/*检测表单中必选的复选框*/
function checkChecked(form) {
var b = true;
$(form).find(".checkbox").each(function (i) {
var ck = $(this)[0].checked;
if (!ck) {
$(this).parents(".form").find(".warn").text($(this).attr("nullmsg"));
$(this).parents(".form").find(".errorMessage").show();
$(this).select();
$(this).focus();
return b = false;
}
});
return b;
}
//检查是否匹配该正则表达式
function GetFlase(value, reg, ele) {
if (reg.test(value)) {
return true;
}
$(ele).parents(".form").find(".warn").text($(ele).attr("logicmsg"));
$(ele).parents(".form").find(".errorMessage").show();
$(ele).focus();
$(ele).select();
return false; //不能提交
}
function CheckInputRex(form) {
var b = true;
$(form).find("input[type='text']").each(function () {
if (typeof ($(this).attr("regex")) == 'string') {
if ($.trim($(this).val()).length > 0 && $(this).val() != this.defaultValue) {
//当前表单的值
var value = $(this).attr("value") || $(this).val();
var regx = eval($(this).attr("regex"));
return b = GetFlase(value, regx, this);
}
}
});
return b;
}
///检查用户输入的相应的字符是否合法
///此方法已废弃
function CheckInput(form) {
var b = true;
$(form).find(".need").each(function () {
if ($.trim($(this).val()).length > 0 && $(this).val() != this.defaultValue) {
//当前表单的值
var value = $(this).attr("value");
//id的值或者name的属性的值如:[name="contact"]
var name = $(this).attr("class");
//检查需要输入的内容是否合法如:联系方式
var len = name.split(" ");
for (var i = 0; i switch ($.trim(len[i])) {
///联系方式
case "mobile":
var reg = /^1\d{10}$/;
return b = GetFlase(value, reg, this);
break;
///邮箱
case "email":
var reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
return b = GetFlase(value, reg, this);
break;
///两次密码是否一致
case "password":
break;
case "password2":
if ($("#password").attr("value") != $("#password2").attr("value")) {
$(this).select(); //获取焦点
$(this).parents(".form").find(".warn").text($(this).attr("logicmsg"));
$(this).parents(".form").find(".errorMessage").show();
return b = false; //不能提交
}
break;
case "worktel":
case "hometel": //家庭电话
var reg = /^\d{8}$/;
return b = GetFlase(value, reg, this);
break;
case "post": //邮编
var reg = /^\d{6}$/;
return b = GetFlase(value, reg, this);
break;
case "bonus":
case "allowance":
case "FixedSalary":
var reg = /^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0|[1-9]\d)$/;
return b = GetFlase(value, reg, this);
break;
case "identity":
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return b = GetFlase(value, reg, this);
break;
case "height":
var reg = /^[1-2][0-9][0-9]$/;
return b = GetFlase(value, reg, this);
break;
case "qq":
var reg = /^[1-9][0-9]{4,}$/;
return b = GetFlase(value, reg, this);
break;
case "begintime":
case "endtime":
var reg = /^\d{4}$/;
if (reg.test(value) && (parseInt($(".endtime").val()) > parseInt($(".begintime").val()))) {
return b;
}
$.ligerDialog.alert($(this).attr("msg"))
$(this).select(); //获取焦点
return b = false; //不能提交
break;
case "num":
var reg = /^\d+$/;
return b = GetFlase(value, reg, this);
break;
///大陆去香港需要办理往来港澳通行证和香港的签注.因私普通护照号码格式有:
///14/15+7位数,G+8位数;
///因公普通的是:P.+7位数;
///公务的是:S.+7位数 或者
//S+8位数,以D开头的是外交护照
case "postport": //护照号码
var reg = /^(P\d{7}|G\d{8}|S\d{7,8}|D\d+|1[4,5]\d{7})$/;
return b = GetFlase(value, reg, this);
break;
case "bankaccount":
var reg = /^[0-9]{19}$/;
return b = GetFlase(value, reg, this);
break;
} //switch
} //for
}
});
return b;
}
///此方法已经废弃
});
///单击改变背景颜色
$(document).ready(function () {
var inputs = $("#top>.c>input");
$(inputs).each(function () {
this.onclick = function () {
document.getElementById("main").style.backgroundColor = this.name;
//$("#main").backgroundColor = this.name;
}
});
});
以上代码就是封装过之后的万能检测表单的方法了,希望小伙伴们喜欢

C 和JavaScript通过WebAssembly实现互操作性。1)C 代码编译成WebAssembly模块,引入到JavaScript环境中,增强计算能力。2)在游戏开发中,C 处理物理引擎和图形渲染,JavaScript负责游戏逻辑和用户界面。

JavaScript在网站、移动应用、桌面应用和服务器端编程中均有广泛应用。1)在网站开发中,JavaScript与HTML、CSS一起操作DOM,实现动态效果,并支持如jQuery、React等框架。2)通过ReactNative和Ionic,JavaScript用于开发跨平台移动应用。3)Electron框架使JavaScript能构建桌面应用。4)Node.js让JavaScript在服务器端运行,支持高并发请求。

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)