Skip to content

Commit 183c33e

Browse files
committed
add
js表单非空验证demo
1 parent 845a2c3 commit 183c33e

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

javascript/Validate Form.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* js公共函数 表单验证非空
3+
* 注意:在使用本文件中函数的时候需要在加载本文件之前先加载jquery库文件
4+
*
5+
* @author:nongfeidan
6+
* @date:2015-04-03
7+
*/
8+
// JavaScript Document
9+
10+
//把需要验证的input加上一个class,通过class获取需要验证的input,如下:
11+
var aVerifi = $('.verification input');//获取所有的验证对象
12+
var aPrompt = $('.verification span');//获取所有的提示信息对象
13+
14+
//获取焦点触发的事件,提示信息消失
15+
aVerifi.each(function ( index ){
16+
$(this).focus(function (){
17+
aPrompt.eq(index).html('');
18+
})
19+
20+
})
21+
//光标离开触发的事件,如为空提示信息
22+
aVerifi.each(function ( index ){
23+
$(this).blur(function (){
24+
if(aVerifi.eq(index).val() == '')
25+
aPrompt.eq(index).html('请填写信息');
26+
aPrompt.eq(index).css('color','red');
27+
})
28+
29+
})
30+
31+
//表单提交事件,调用非空函数
32+
$('#form').submit(function(){
33+
return checkSubmit( aVerifi,aPrompt ); // 第一个参数是判断非空的对象 ,第二个是提示信息
34+
})
35+
//验证非空函数
36+
function checkSubmit( obj1, obj2 ){
37+
var arr =[];
38+
obj1.each(function ( index ){
39+
if(obj1.eq(index).val()==''){
40+
obj2.eq(index).html('请填写信息');
41+
obj2.eq(index).css('color','red');
42+
}
43+
if(obj1.eq(index).val() !==''){
44+
arr.push(1);
45+
}
46+
47+
})
48+
if( arr.length !== obj1.length){
49+
return false;
50+
}
51+
}

javascript/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
common.js ----- 工作中用到的公共js文件
1+
common.js ----- 工作中用到的公共js文件
22
pic_lazyload_demo ----- 图片延时加载demo
3+
ValidateForm.js ----- 表单非空验证

0 commit comments

Comments
 (0)