封裝好的一個(gè)萬能檢測表單的方法
檢測表單中的不能為空(.notnull)的驗(yàn)證
作用:一對form標(biāo)簽下有多個(gè)(包括一個(gè))表單需要提交時(shí),使用js準(zhǔn)確的判斷當(dāng)前按鈕對那些元素做判斷
用法:在form標(biāo)簽下 找到當(dāng)前 表單的容器 給予class="form",當(dāng)前表單的提交按鈕給予 class="check"
需要驗(yàn)證為空的元素給予class="notnull" nullmsg="xx不能為空!"提示,需要進(jìn)行邏輯判斷的表單給予class="need"
判斷的類型給予 class="num"(只能是數(shù)字) 驗(yàn)證的提示 logicmsg="XX只能是數(shù)字"
給予class="errorMessage"顯示錯(cuò)誤信息塊
給予class="warn"顯示錯(cuò)誤信息
未使用js面向?qū)ο缶幊?br />
邏輯判斷,不傳入need標(biāo)識,直接給出正則表達(dá)式屬性(自定義)regex="/^\d$/" 做出判斷
在外部實(shí)現(xiàn)
Global.submitCallback button回調(diào)函數(shù)
Global.confirmCallback confirm回調(diào)函數(shù);
需要改進(jìn)的地方:
暫無
/// <reference path="vendor/jquery-1.4.1-vsdoc.js" />
*/
//$(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);
// }
// }
// });
// }
//);
///*封裝一個(gè)萬能檢測表單的方法*/
///event.srcElement:引發(fā)事件的目標(biāo)對象,常用于onclick事件。
///event.fromElement:引發(fā)事件的對象源,常用于onmouseout和onmouseover事件。
///event.toElement:引發(fā)事件后,鼠標(biāo)移動到的目標(biāo)源,常用于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 <= 0) {//|| $(this).val() == this.defaultValue
// 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;
}
/*檢測表單中必選的復(fù)選框*/
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;
}
//檢查是否匹配該正則表達(dá)式
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) {
//當(dāng)前表單的值
var value = $(this).attr("value") || $(this).val();
var regx = eval($(this).attr("regex"));
return b = GetFlase(value, regx, this);
}
}
});
return b;
}
///檢查用戶輸入的相應(yīng)的字符是否合法
///此方法已廢棄
function CheckInput(form) {
var b = true;
$(form).find(".need").each(function () {
if ($.trim($(this).val()).length > 0 && $(this).val() != this.defaultValue) {
//當(dāng)前表單的值
var value = $(this).attr("value");
//id的值或者name的屬性的值如:[name="contact"]
var name = $(this).attr("class");
//檢查需要輸入的內(nèi)容是否合法如:聯(lián)系方式
var len = name.split(" ");
for (var i = 0; i < len.length; i++) {
switch ($.trim(len[i])) {
///聯(lián)系方式
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(); //獲取焦點(diǎn)
$(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(); //獲取焦點(diǎn)
return b = false; //不能提交
break;
case "num":
var reg = /^\d+$/;
return b = GetFlase(value, reg, this);
break;
///大陸去香港需要辦理往來港澳通行證和香港的簽注.因私普通護(hù)照號碼格式有:
///14/15+7位數(shù),G+8位數(shù);
///因公普通的是:P.+7位數(shù);
///公務(wù)的是:S.+7位數(shù) 或者
//S+8位數(shù),以D開頭的是外交護(hù)照
case "postport": //護(hù)照號碼
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;
}
///此方法已經(jīng)廢棄
});
///單擊改變背景顏色
$(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;
}
});
});
以上代碼就是封裝過之后的萬能檢測表單的方法了,希望小伙伴們喜歡
相關(guān)文章
詳解用函數(shù)式編程對JavaScript進(jìn)行斷舍離
本篇文章主要介紹了用函數(shù)式編程對JavaScript進(jìn)行斷舍離,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09Javascript實(shí)現(xiàn)仿WebQQ界面的“浮云”兼容 IE7以上版本及FF
兼容:IE7以上版本及FF;(騰訊的WebQQ3.0好像也不兼容IE6,其實(shí)這樣挺好的)2011-04-04使用JavaScript和HTML實(shí)現(xiàn)一個(gè)精美的計(jì)算器
計(jì)算器是我們?nèi)粘I钪薪?jīng)常使用的工具之一,可以幫助我們進(jìn)行簡單的數(shù)學(xué)運(yùn)算,在本博文中,我將使用JavaScript編寫一個(gè)漂亮的計(jì)算器,并添加加減乘除功能,感興趣的同學(xué)可以自己動手嘗試一下2023-09-09二級域名或跨域共享Cookies的實(shí)現(xiàn)方法
適用于Asp。 在主域名設(shè)置的Cookie,在各子域名共用;適用于博客等提供二級域名。這個(gè)問題,以網(wǎng)上有眾多帖子,可惜都沒有完整解決。2008-08-08JavaScript編寫開發(fā)動態(tài)時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了JavaScript編寫開發(fā)動態(tài)時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07JS仿Windows開機(jī)啟動Loading進(jìn)度條的方法
這篇文章主要介紹了JS仿Windows開機(jī)啟動Loading進(jìn)度條的方法,實(shí)例分析了javascript操作html元素及對應(yīng)樣式實(shí)現(xiàn)特效的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02