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