jQuery 表單驗證擴(kuò)展代碼(一)
一. 分析表單驗證的基本情況
在我們做web開發(fā)的過程中,會遇到各種各樣的驗證。歸納一下基本可以分為一下幾類:
(1). 是否必填項 [這個是非?;镜腯
(2). 輸入?yún)?shù)中的范圍校驗
(3). 輸入?yún)?shù)與另外一個控件值的比較
(4). 輸入的參數(shù)正則表達(dá)式驗證
二. 是否必填項驗證
有如下幾種情況:
(1) 輸入框獲得焦點(diǎn)提示
(2)輸入框失去焦點(diǎn)驗證錯誤提示
(3)輸入框失去焦點(diǎn)驗證正確提示
首先確定輸入框是否是必填項,然后就是提示消息的現(xiàn)實位置。
根據(jù)以上幾種情況確定如下幾個參數(shù):
required : 是否為必填項,true 和 false ,true 表示為必填項 (*)
onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
tipId : 用于顯示提示信息的控件id (*)
組合例子 : {required:true,onFocus:"Required",onBlur:"@error",onSucces:"Success",tipId:"tipid"}
注意: 上面定義的一些規(guī)則,有些可能沒有實現(xiàn),在后期過程中逐漸完善。
/**
* 檢查輸入框是否為必填項
* 輸入?yún)?shù):
* required : 是否為必填項,true 和 false ,true 表示為必填項 (*)
* onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
* onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* tipId : 用于顯示提示信息的控件id (*)
* 組合例子 : {required:true,onFocus:"Required",onBlur:"@error",onSucces:"Success",tipId:"tipid"}
*/
$.fn.extend({
checkRequired:function(inputArg){
if(inputArg.required){
if($(this).is("input") || $(this).is("textarea")){
//綁定獲得焦點(diǎn)事件
$(this).bind("focus",function(){
if(inputArg.onFocus!=undefined){
$("#" + inputArg.tipId).html(inputArg.onFocus);
}
});
//綁定失去焦點(diǎn)事件
$(this).bind("blur",function(){
if($(this).val()!=undefined && $(this).val()!=""){
$("#" + inputArg.tipId).html(inputArg.onSucces);
}else{
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
});
}
}
}
});
使用效果和測試代碼:
當(dāng)獲得焦點(diǎn)時候后面提示效果
當(dāng)失去焦點(diǎn)沒有輸入提示效果
當(dāng)輸入文本信息之后提示成功效果
測試代碼如下:
<script language="JavaScript" src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script language="JavaScript" src="jquery-extend-1.0.0.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
$(document).ready(function(){
$("#txtName").checkRequired({
required:true,
onFocus:"這個為必填項",
onBlur:"必須填寫啊",
onSucces:"恭喜,你輸入了",
tipId:"txtNameTip"
});
});
</script>
三. 輸入?yún)?shù)中的范圍校驗
相比上面的驗證來說,這個稍微復(fù)雜了一些,因為有輸入值的范圍。校驗做了如下區(qū)分:輸入的數(shù)據(jù)類型為 字符串, 數(shù)字 ,時間
如果是字符串則比較字符串的長短,數(shù)字和時間比較大小。(時間目前沒有完善)
因為比較范圍所以定義一個區(qū)間范圍,所以這里再添加兩個屬性,下限值和上限值。
輸入?yún)?shù)列表:
onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onEmpty : 輸入項為空文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
dataType : 數(shù)據(jù)類型參數(shù)(text,number,date)
min : 輸入的最小值
max : 輸入的最大值
tipId : 用于顯示提示信息的控件id (*)
/**
* 檢查輸入項的范圍
* 輸入?yún)?shù):
* onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onEmpty : 輸入項為空文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
* dataType : 數(shù)據(jù)類型參數(shù)(text,number,date)
* min : 輸入的最小值
* max : 輸入的最大值
* tipId : 用于顯示提示信息的控件id (*)
*
*/
$.fn.extend({
checkRange:function(inputArg){
if ($(this).is("input") || $(this).is("textarea")) {
//獲得焦點(diǎn)綁定
$(this).bind("focus",function(){
if(inputArg.onFocus!=undefined){
$("#" + inputArg.tipId).html(inputArg.onFocus);
}
});
//失去焦點(diǎn)綁定
$(this).bind("blur",function(){
if($(this).val()==undefined || $(this).val()==""){
$("#" + inputArg.tipId).html(inputArg.onEmpty);
}else{
switch(inputArg.dataType){
case "text":
if($(this).val().length>= parseInt(inputArg.min) && $(this).val().length< parseInt(inputArg.max)){
$("#" + inputArg.tipId).html(inputArg.onSucces);
}else{
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
case "number":
if(!isNaN($(this).val())){
if(parseInt($(this).val())>parseInt(inputArg.min) && parseInt($(this).val())<parseInt(inputArg.max)){
$("#" + inputArg.tipId).html(inputArg.onSucces);
}else{
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
}
break;
case "date":
break;
}
}
});
}
}
});
輸入項范圍效果和測試代碼
如果年齡約定為數(shù)字
輸入不在約定范圍之內(nèi)
驗證成功
$("#txtAge").checkRange({
onFocus:"年齡為數(shù)字",
onEmpty:"不能為空啊",
onSucces:"驗證成功",
onBlur:"驗證失敗,請認(rèn)真輸入",
dataType:"number",
min:"10",
max:"100",
tipId:"txtAgeTip"
});
<p>
<label>年齡:</label><input type="text" id="txtAge" value=""/><span id="txtAgeTip"></span>
</p>
四. 輸入?yún)?shù)與另外一個控件值的比較
和上面的驗證比較,不同的地方在于要指定比較控件的id
下面是輸入?yún)?shù):
onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onEmpty : 輸入項為空文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
dataType : 數(shù)據(jù)類型參數(shù)(text,number,date)
comType : 比較的類型(=,>,>=,<,<=,!=)
tipId : 用于顯示提示信息的控件id (*)
targetId : 比較的目標(biāo)控件Id
/**
* 控件值之間的比較
* 輸入?yún)?shù):
* onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onEmpty : 輸入項為空文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
* dataType : 數(shù)據(jù)類型參數(shù)(text,number,date)
* comType : 比較的類型(=,>,>=,<,<=,!=)
* tipId : 用于顯示提示信息的控件id (*)
* targetId : 比較的目標(biāo)控件Id
*/
$.fn.extend({
checkCompare:function(inputArg){
if($(this).is("input") || $(this).is("textarea")){
//獲得焦點(diǎn)綁定
$(this).bind("focus",function(){
if(inputArg.onFocus!=undefined){
$("#" + inputArg.tipId).html(inputArg.onFocus);
}
});
//失去焦點(diǎn)綁定
$(this).bind("blur",function(){
var targetValue=$("#"+inputArg.targetId).val();
if(targetValue!=undefined && targetValue!=null){
if($(this).val()!=undefined && $(this).val()!=""){
if(inputArg.dataType=="text"){
switch(inputArg.comType){
case "=":
if(targetValue==$(this).val()){
$("#" + inputArg.tipId).html(inputArg.onSucces);
}else{
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
case "!=":
if(targetValue!=$(this).val()){
$("#" + inputArg.tipId).html(inputArg.onSucces);
}else{
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
}
}else if(inputArg.dataType=="number"){
if (isNaN(targetValue) == false && isNaN($(this).val()) == false) {
switch (inputArg.comType) {
case "=":
if (targetValue == $(this).val()) {
$("#" + inputArg.tipId).html(inputArg.onSucces);
}
else {
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
case "!=":
if (targetValue != $(this).val()) {
$("#" + inputArg.tipId).html(inputArg.onSucces);
}
else {
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
case ">":
if ($(this).val() > targetValue) {
$("#" + inputArg.tipId).html(inputArg.onSucces);
}
else {
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
case ">=":
if ($(this).val() >= targetValue) {
$("#" + inputArg.tipId).html(inputArg.onSucces);
}
else {
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
case "<":
if ($(this).val() < targetValue) {
$("#" + inputArg.tipId).html(inputArg.onSucces);
}
else {
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
case "<=":
if ($(this).val() <= targetValue) {
$("#" + inputArg.tipId).html(inputArg.onSucces);
}
else {
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
break;
}
}else{
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
}else if(inputArg.dataType=="date"){
}
}else{
$("#" + inputArg.tipId).html(inputArg.onEmpty);
}
}
});
}
}
});
控件值之間的比較效果和測試代碼
效果圖1
效果圖2
效果圖3
$("#txtPass2").checkCompare({
onFocus:"和前面的比較",
onEmpty:"輸入的不能為空",
onSucces:"驗證成功",
onBlur:"驗證失敗",
dataType:"number",
comType:">=",
tipId:"txtPass2Tip",
targetId:"txtPass1"
});
<p>
<label>密碼1:</label><textarea id="txtPass1"></textarea><span id="txtPass1Tip"></span>
</p>
<p>
<label>密碼2:</label><textarea id="txtPass2"></textarea><span id="txtPass2Tip"></span>
</p>
五. 輸入的參數(shù)正則表達(dá)式驗證
這個驗證相對比較簡單,因為使用正則表達(dá)式,無需自己去思考輸入的情況。只需要引入一個正則表達(dá)式就可以了
下面是輸入?yún)?shù):
onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onEmpty : 輸入項為空文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
regExp : 正則表達(dá)式
tipId : 用于顯示提示信息的控件id (*)
jQuery正則表達(dá)式的驗證
/**
* 正則表達(dá)式的驗證
* 輸入?yún)?shù):
* onFocus : 獲得焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onEmpty : 輸入項為空文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onSucces : 驗證成功的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)
* onBlur : 失去焦點(diǎn)的文字提示(如果指定樣式則在樣式名前加 @ ,因此文字提示首字母不能有@)(驗證失敗提示)
* regExp : 正則表達(dá)式
* tipId : 用于顯示提示信息的控件id (*)
*/
$.fn.extend({
checkRegExp:function(inputArg){
if ($(this).is("input") || $(this).is("textarea")) {
//獲得焦點(diǎn)綁定
$(this).bind("focus", function(){
if (inputArg.onFocus != undefined) {
$("#" + inputArg.tipId).html(inputArg.onFocus);
}
});
//獲得失去焦點(diǎn)事件
$(this).bind("blur",function(){
if($(this).val()!=undefined && $(this).val()!=""){
if ($(this).val().match(inputArg.regExp) == null) {
$("#" + inputArg.tipId).html(inputArg.onSucces);
}else{
$("#" + inputArg.tipId).html(inputArg.onBlur);
}
}else{
$("#" + inputArg.tipId).html(inputArg.onEmpty);
}
});
}
}
});
正則表達(dá)式效果和測試代碼
輸入非數(shù)字
輸入數(shù)字
$("#txtAge").checkRegExp({
onFocus:"年齡必須為數(shù)字",
onEmpty:"輸入的不能為空",
onSucces:"驗證成功",
onBlur:"驗證失敗",
regExp:/\D/,
tipId:"txtAgeTip"
});
<label>年齡:</label><input type="text" id="txtAge" value=""/><span id="txtAgeTip"></span>
這是驗證插件的一個基本雛形,后期不斷跟新..........
相關(guān)文章
jQuery使用siblings獲取某元素所有同輩(兄弟姐妹)元素用法示例
這篇文章主要介紹了jQuery使用siblings獲取某元素所有同輩(兄弟姐妹)元素用法,結(jié)合簡單實例形式分析了siblings()函數(shù)獲取同輩元素的操作技巧,需要的朋友可以參考下2017-01-01jquery+ajaxform+springboot控件實現(xiàn)數(shù)據(jù)更新功能
這篇文章主要介紹了jquery+ajaxform+springboot控件實現(xiàn)數(shù)據(jù)更新操作,使用jquery的ajaxform插件是一個比較不錯的選擇。具體實現(xiàn)工程大家參考下本文2018-01-01jquery獲取復(fù)選框checkbox的值實現(xiàn)方法
下面小編就為大家?guī)硪黄猨query獲取復(fù)選框checkbox的值實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05jQuery實現(xiàn)導(dǎo)航滾動到指定內(nèi)容效果完整實例【附demo源碼下載】
這篇文章主要介紹了jQuery實現(xiàn)導(dǎo)航滾動到指定內(nèi)容效果,結(jié)合完整實例形式分析了頁面元素屬性動態(tài)變換操作相關(guān)技巧,涉及jQuery插件jquery.scrollto.js的使用,并附帶demo源碼下載供讀者下載參考,需要的朋友可以參考下2016-09-09jQuery UI工具提示框部件Tooltip Widget
這篇文章介紹了jQuery UI工具提示框部件Tooltip Widget,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06jQuery實現(xiàn)鼠標(biāo)滑過預(yù)覽圖片大圖效果的方法
這篇文章主要介紹了jQuery實現(xiàn)鼠標(biāo)滑過預(yù)覽圖片大圖效果的方法,涉及jQuery鼠標(biāo)事件響應(yīng)及頁面元素屬性動態(tài)操作相關(guān)技巧,需要的朋友可以參考下2017-04-04jQuery實現(xiàn)鼠標(biāo)經(jīng)過時高亮,同時其他同級元素變暗的效果
這篇文章主要介紹了jQuery實現(xiàn)鼠標(biāo)經(jīng)過時高亮,同時其他同級元素變暗的效果,涉及jQuery基于事件響應(yīng)機(jī)制的頁面元素遍歷與屬性變換操作技巧,需要的朋友可以參考下2016-09-09