JavaScript Regular Expression Validation function code _ Regular expression

Source: Internet
Author: User
Tags getdate
Using regular expressions to determine whether the Arabic numerals are 0-9
Copy Code code as follows:

function Regisdigit (fdata)
{
var reg = new RegExp ("^[0-9]$");
Return (Reg.test (fdata));
}

Use this expression to get the length of the string
Copy Code code as follows:

function Regdatalength (fdata)
{
var vallength = fdata.length;
var reg = new RegExp ("^[\u0391-\uffe5]$");
var result = 0;
for (i=0; i< vallength; i++)
{
if (Reg.test (Fdata.charat (i)))
{
result = 2;
}
Else
{
Result + +;
}
}
return result;
}

Apply an extension to determine whether a value
Copy Code code as follows:

function Regisnumber (fdata)
{
var reg = new RegExp ("^[-]?[ 0-9]+[\.]? [0-9]+$ ");
Return Reg.test (Fdata)
}

Verify that the email is correct
Copy Code code as follows:

function Regisemail (fdata)
{
var reg = new RegExp ("^[0-9a-za-z]+@[0-9a-za-z]+[\.] {1} [0-9a-za-z]+[\.]? [0-9a-za-z]+$ ");
Return Reg.test (Fdata);
}

Determine if the phone number is correct
Copy Code code as follows:

function Regisphone (fdata)
{
var reg =/^ (\+86)? (1[0-9]{10}) $/;
Return Reg.test (Fdata);
}


Determine if the input is a string consisting of a 0-9/A-z/A-Z
Copy Code code as follows:

function Isalphanumber (str)
{
var result=str.match (/^[a-za-z0-9]+$/);
if (result==null) return false;
return true;
}


Determines whether the input is a number--(numbers contain decimals)--
Copy Code code as follows:

function Isnumber (str)
{
return!isnan (str);
}


Determines whether the input is an integer
Copy Code code as follows:

function Isint (str)
{
var result=str.match (/^ (-|\+)? \d+$/);
if (result==null) return false;
return true;
}


Determine if the input is a valid long date format-"Yyyy-mm-dd HH:MM:SS" | | "Yyyy/mm/dd HH:MM:SS"
Copy Code code as follows:

function Isdatetime (str)
{
var result=str.match (/^ (\d{4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2);
if (result==null) return false;
var d= new Date (result[1], result[3]-1, result[4], result[5], result[6], result[7]);
Return (D.getfullyear () ==result[1]&& (D.getmonth () +1) ==result[3]&&d.getdate () ==result[4]& &d.gethours () ==result[5]&&d.getminutes () ==result[6]&&d.getseconds () ==result[7]);
}


Check for Yyyy-mm-dd | | Date format for YYYY/MM/DD
Copy Code code as follows:

function IsDate (str) {
var result=str.match (/^ (\d{4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
if (result==null) return false;
var d=new Date (Result[1], result[3]-1, result[4]);
Return (D.getfullyear () ==result[1] && d.getmonth () +1==result[3] && d.getdate () ==result[4]);
}


Determine if the input is a valid e-mail message
Copy Code code as follows:

function Isemail (str)
{
var result=str.match (-\w+) | (/^\w+) | ( \.\w+)) *\@[a-za-z0-9]+ (\.| -) [a-za-z0-9]+) *\. [a-za-z0-9]+$/);
if (result==null) return false;
return true;
}


Remove the trailing spaces of a string
Copy Code code as follows:

function Trim (str) {
Return Str.replace (/(^\s*) | ( \s*$)/g, "");
}


Returns the actual length of the string, one Chinese character counts 2 lengths
Copy Code code as follows:

function strlen (str) {
Return Str.replace (/[^\x00-\xff]/g, "* *"). Length;
}


Matching China ZIP code (6-bit)
Copy Code code as follows:

function Ispostcode (str)
{
var result=str.match (/[1-9]\d{5} (?!) \d)/);
if (result==null) return false;
return true;
}

Match domestic phone number (0511-4405222 or 021-87888822)
Copy Code code as follows:

function Istell (str)
{
var result=str.match (/\d{3}-\d{8}|\d{4}-\d{7}/);
if (result==null) return false;
return true;
}

A checksum is an integer (0-10000)
Copy Code code as follows:

function Isint1 (str)
{
var result=str.match (/^[0-9]$|^ ([1-9]) ([0-9]) {0,3}$|^10000$/);
if (result==null) return false;
return true;
}


Match Tencent QQ number
Copy Code code as follows:

function isqq (str)
{
var result=str.match (/[1-9][0-9]{4,}/);
if (result==null) return false;
return true;
}


Matching ID (15-bit or 18-bit)
Copy Code code as follows:

function Isidcard (str)
{
var result=str.match (/\d{15}|\d{18}/);
if (result==null) return false;
return true;
}

Verify that the text is empty
Copy Code code as follows:

function Checknull (field,sval)
{
if (Field.value = "")
{
Alert ("Please fill in" + Sval +)! ");
Field.focus ();
return false;
}
return true;
}


Mask input Characters
Copy Code code as follows:

function Checkchar ()
{
var keycode = Event.keycode;
if (!) ( keycode>=48&&keycode<=57))
{
return false;
}
}
Related Article

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.