Home > Web Front-end > JS Tutorial > body text

Jquery form validation class introduction and examples_jquery

WBOY
Release: 2016-05-16 17:32:36
Original
930 people have browsed it

[html]

复制代码 代码如下:


数值:

浮点型:

英文:

大写英文:

小写英文:

邮件格式:

是否包含中文:

URL:

电话号码:

IP地址:

金额:

数值或者英文或者_:

邮政编码:

可用账号:

QQ:

身份证:

数值 允许为空:

数值 长度 1-3:

数值 长度 1-3 允许为空:







[javascript]
复制代码 代码如下:

/*
* Universal JS validation class
* Usage:
* var formValidate = new formValidate();
* formValidate.init({});
* Note:
*

* id is formValidate
*
*
* validate="zip_code" Verify whether it is a zip code
* empty ="yes" Verify whether empty is allowed
* min=10 minimum length
* max=10 maximum length
* display prompt content
*/
var formValidate = function () {

var _this = this;

this.options = {
number : {reg : /^[0-9] $/, str : 'Must be a number'},
decimal : {reg : /^[-]{0,1}(d )[.] (d )$/ , str : 'Must be in DECIMAL format'},
english : {reg : /^[A-Za-z] $/, str : 'Must be English letters'},
upper_english : {reg : /^[A-Z] $/, str : 'Must be uppercase English letters' letters'},
lower_english : {reg : /^[a-z] $/, str : 'Must be lowercase English letters'},
email : {reg : /^([a-zA-Z0-9 ] [_|_|.]?)*[a-zA-Z0-9] @([a-zA-Z0-9] [_|_|.]?)*[a-zA-Z0-9] .[a-zA-Z]{2,3}$/, str : 'Email format is incorrect'},
chinese : {reg : /[u4E00-u9FA5uf900-ufa2d]/ig, str : 'Must contain Chinese'},
url : {reg : /^[a-zA-z] ://[^s]*/, str : 'URL format is incorrect'},
phone : {reg : / ^[1][3][0-9]{9}$/ , str : 'Phone number format is incorrect'},
ip : {reg : /^(d ).(d ).(d ) .(d )$/ , str : 'IP address format is incorrect'},
money : {reg : /^[0-9] [.][0-9]{0,3}$/ , str : 'The amount format is incorrect'},
number_letter : {reg : /^[0-9a-zA-Z_] $/ , str : 'Only English letters, numbers, and _ are allowed to be entered'},
zip_code : {reg : /^[a-zA-Z0-9 ]{3,12}$/ , str : 'Postal code format is incorrect'},
account : {reg : /^[a-zA-Z ][a-zA-Z0-9_]{4,15}$/ , str : 'The account name is illegal, 5-16 characters, letters, underscores and numbers are allowed'},
qq : {reg : /[1 -9][0-9]{4,}/ , str : 'QQ account is incorrect'},
card : {reg : /^(d{6})(18|19|20)?(d {2})([01]d)([0123]d)(d{3})(d|X)?$/ , str : 'ID number is incorrect'},
};

//Initialize bound form options
this.init = function (options) {
this.setOptions(options);
this.checkForm();
};

//Set parameters
this.setOptions = function (options) {
for (var key in options) {
if (key in this.options) {
this.options[key] = options[key];
}
}
};

//Detect whether the form is empty, maximum and minimum values, regular verification
this.checkForm = function ( ) {
$("#formValidate").submit(function () {
var formChind = $("#formValidate").children();
var testResult = true;
formChind. each(function (i) {
var child = formChind.eq(i);
var value = child.val();
var len = value.length;
var childSpan = child. next();

//Whether the attribute is empty
if (child.attr('empty')) {
if (child.attr('empty') == ' yes' && value == '') {
if (childSpan) {
childSpan.html('');
}
return;
}
}

//The maximum and minimum lengths of min and max in the attributes
var min = null;
var max = null;
if (child.attr('min')) min = child.attr(' min');
if (child.attr('max')) max = child.attr('max');
if (min && max) {
if (len < min || len > max) {
if (childSpan) {
childSpan.html('');
childSpan.html(' The string length is between ' min ' and ' max ');
testResult = false;
return;
}
}
} else if (min) {
if (len < min) {
if (childSpan) {
childSpan.html('');
childSpan.html(' String length should be greater than ' min);
testResult = false;
return;
}
}
} else if (max) {
if (len > max) {
if (childSpan) {
childSpan.html('');
childSpan.html(' The string length should be less than ' max);
testResult = false;
return;
}
}
}

//Regular verification
if (child.attr('validate' )) {
var type = child.attr('validate');
var result = _this.check(value, type);
if (childSpan) {
childSpan.html('' );
if (result != true) {
childSpan.html(' ' result);
testResult = false;
}
}
}

});
return testResult;
});
};

//Detect a single regular option
this.check = function (value, type) {
if (this.options[type]) {
var val = this.options[type]['reg'];
if (!val.test(value)) {
return this.options[type ]['str'];
}
return true;
} else {
return 'The form validation regular item cannot be found';
}
};


}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template