JavaScript Universal validation function library (phone, mailbox, mobile, digital)

Source: Internet
Author: User
Tags zip

Web page Special Effects Universal Verification function library (telephone, mailbox, mobile phone, number)
This verification library is our commonly used telephone, mailbox, mobile phone, digital QQ, letters, Chinese, English and other validation functions.

/*
@author: Slchen
@exemple:
var email= "slchen@xxxxx.com";
Alert ($v. Isemail (email));
*/
var $v = (function () {

Function.prototype.method = function (name, FN) {

This.prototype[name] = fn;

};

var validator=function () {

};

Whether Mail
Validator.method (' Isemail ', function (obj) {

var re=new RegExp (Validatorregex.email);

return re.test (obj);
});

Telephone, phone number format for country code (2 to 3 bits)-area code (2 to 3 digits)-Phone number (7 to 8 digits)-extension (3 digits)
Validator.method (' Istelephone ', function (obj) {

var re=new RegExp (Validatorregex.telephone);

return re.test (obj);
});

Whether the mobile phone number
Validator.method (' IsMobile ', function (obj) {

var re=new RegExp (Validatorregex.mobile);

return re.test (obj);
});


Whether the URL
Validator.method (' Isurl ', function (obj) {

var re=new RegExp (Validatorregex.url);

return re.test (obj);
});

Whether ID card
Validator.method (' Isidcard ', function (obj) {
var num=obj.tolowercase (). Match (/./g);

if (Obj.match (/^d{17}[dx]$/i)) {

var sum=0,times=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];

            for (var i=0;i<17;i++)
                 sum+=parseint (num[i],10) *times[i ];
            if ("10x98765432". CharAt (sum%11)!=num[17]
                return false;
            return!! Obj.replace (/^d{6} (D{4}) (D{2}) (D{2}). +$/, "$1-$2-$3"). Isdatetime ();
       }
        if (Obj.match (/^d{15}$/))
             return!! Obj.replace (/^d{6} (D{2}) (D{2}) (D{2}). +$/, "19$1-$2-$3"). Isdatetime ();
        return false;
});

//Whether the date
Validator.method (' IsDate ', function (obj) {
        var date=obj ===false?obj:obj.parsestddate (FALSE);
        if (!date) return false;
        var arr=date.match (/^ (19|20) d{2})-(d{1,2})-(d{1,2}) $/);
        if (!arr) return false;
        for (var i=1;i<5;i++)
             Arr[i]=parseint (arr[i],10);
        if (arr[3]<1| | arr[3]>12| | arr[4]<1| | ARR[4]>31) return false;
        var _t_date=new date (arr[1],arr[3]-1,arr[4]);
        return _t_date.getdate () ==arr[4]?_t_date:null;
});

is empty
Validator.method (' IsNullOrEmpty ', function (obj) {
if (!obj| | obj== ' null ' | | typeof (obj) ==undefined | | obj== "" | | obj.length==0) {
return true;
}
else{
return false;
}
});

Whether it is an integer
Validator.method (' Isinteger ', function (obj) {
var num=new RegExp (Validatorregex.integer);
if (isNaN (obj)) {
return False
}
else{
return num.test (obj);
}
});


Whether it is a decimal
Validator.method (' Isdecimal ', function (obj) {
if (isNaN (obj)) {
return False
}
else{
var num=new RegExp (Validatorregex.decimal);

return num.test (obj);
}
});


is a positive integer
Validator.method (' Ispositiveinteger ', function (obj) {

if (isNaN (obj)) {
return False
}
else{
var num=new RegExp (Validatorregex.positiveinteger);

return num.test (obj);
}
});

is a positive decimal
Validator.method (' Ispositivedecimal ', function (obj) {
var num=new RegExp (Validatorregex.positivedecimal);
if (isNaN (obj)) {
return False
}
else {
return num.test (obj);
}
});

Whether it is a postcode
Validator.method (' Iszip ', function (obj) {
var num=new RegExp (Validatorregex.zip);

return num.test (obj);
});

Whether it is QQ
Validator.method (' isqq ', function (obj) {

var num=new RegExp (VALIDATORREGEX.QQ);

return num.test (obj);
});


Whether it is English
Validator.method (' Isenglish ', function (obj) {

var num=new RegExp (Validatorregex.letter);

return num.test (obj);
});


Whether it is Chinese
Validator.method (' Ischinese ', function (obj) {

var num=new RegExp (Validatorregex.chinese);

return num.test (obj);
});

var Validatorregex = {

Email:/^[-_a-za-z0-9]+@ ([_a-za-z0-9]+.) +[a-za-z0-9]{2,3}$/,//Mail

Telephone:/^ ((d{3)) | ( d{3}-))? ((0d{2,3}) |0d{2,3}-)? [1-9]d{6,7} (-d{1,4})? $/,//telephone or fax

Mobile:/^ ((d{3}) | ( d{3}-))? 1d{10}$/,//mobile Phone

URL:/^http://[a-za-z0-9]+. [a-za-z0-9]+[/=?%-&_~ ' @[] ': +!] * ([^<> ""]) *$/,//URL

Decimal:/^[-+]?d+ (. d+)? $/,//decimal

Integer:/^[-+]?d+$/,//Integer

Positivedecimal:/^d+ (. d+)? $/,//Decimal

Positiveinteger:/^d+$/,//positive integer

Zip:/^[1-9]d{5}$/,//ZIP/Postal Code

QQ:/^[1-9]D{4,8}$/,//QQ

Letter:/^[a-za-z]+$/,//English alphabet

Chinese:/^[u0391-uffe5]+$///Chinese
}

return new Validator ();

})();

Method Description:


Method Description:
Verify that the message is:

$v. Isemail (obj);

Verify that the phone is not:

$v. Istelephone (obj);

Verify that your mobile number is:

$v. IsMobile (obj);

Verify that the URL is:

$v. Isurl (obj);

Verify if ID:

$v. Isidcard (obj);

Verify If Date:

$v. IsDate (obj);

Verify is null:

$v. IsNullOrEmpty (obj);

Verify is an integer:

$v. Isinteger (obj);

Verify that it is a decimal:

$v. Isdecimal (obj);

To verify whether a positive integer:

$v. Ispositiveinteger (obj);

Verify if a positive decimal:

$v. Ispositivedecimal (obj);

Verify the ZIP code:

$v. Iszip (obj);

Verify is QQ:

$v. ISQQ (obj);

Verify if it is in English:

$v. Isenglish (obj);

Whether it is Chinese:

$v. Ischinese (obj);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.