JS various commonly used forms to verify regular functions (numbers, mailboxes, integers, decimals)

Source: Internet
Author: User
Tags eval

JS various commonly used forms to verify regular functions (numbers, mailboxes, integers, decimal) dates, etc.

/************************************************
Validating various data formats
*************************************************/

Add to the number,

function Formatnum (v) {
var reg =/(? = (?! b) (DDD) + (?! d)/g;
Return V.replace (Reg, ', ');
}

Length is not 0 (full space is also considered valid)

function Notnull (v) {
return v.length > 0;
}

Validates a floating-point number (must be a full floating-point number or integer, such as 1.0 or 1, not. 1 or 1.)

function C_float (v) {
var reg =/^d+ (. d+)? $/;
return Reg.test (v);
}


validating integers

function C_int (v) {
var reg =/^d+$/;
return Reg.test (v);
}


Verifying mailboxes

function C_mail (v) {
var reg =/^w+ (-w+) | (. w+)) *@[a-za-z0-9]+ ((. | -) [a-za-z0-9]+] *. [a-za-z0-9]+$/;
return Reg.test (v);
}


Verify ZIP code, consisting of 6 digits

function C_postalcode (v) {
return/^d{6}$/.test (v);
}


Verification Code

function C_validatecode (v) {
return/^w{4}$/.test (v);
}

To verify a string in the format "2008-01-01"

Function C_datestr (val) {
if (val = = "") return true; Birthdays are not required

var reg =/^d{4}-d{1,2}-d{1,2}$/;
if (!reg.test (Val)) return false;

var y,m,d;
var date_list = Val.split ("-");
y = eval (date_list[0]);
m = eval (date_list[1]);
D = eval (date_list[2]);
if (y<1 | | m<1 | | m>12 | | d<1 | | y>2900 | | y<1903) return FALSE;

var month_day = [0,31,28,31,30,31,30,31,31,30,31,30,31];

if (D>month_day[m]) {
if (m==2) {
if ((y%100!=0 && y%4==0) | | y%400==0) && d<30)
return true;
}else{
return false;
}
}else return true;
}

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.