JS Common Custom Function Summary _ Basics

Source: Internet
Author: User
Tags pow
Copy Code code as follows:

String.prototype.trim = function () {
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}

Check to see if it's a date format
function IsDate (DATESTR) {
var result = Datestr.match (/(^ (1[8-9]\d{2}) | ( [2-9]\d{3})] (-) (10|12|0?[ 13578]) (-) (3[01]|[ 12][0-9]|0? [1-9]) $)| (^ (1[8-9]\d{2}) | ([2-9]\d{3})] (-) (11|0?) [469]) (-) (30| [12] [0-9]|0? [1-9]) $)| (^ (1[8-9]\d{2}) | ([2-9]\d{3})] (-) (0?2) (-) (2[0-8]|1[0-9]|0?) [1-9]) $)| (^ ([2468][048]00) (-) (0?2) (-) (29) $) | (^ ([3579][26]00) (-) (0?2) (-) (29) $) | (^ ([1][89][0][48]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][0][48]) (-) (0?2) (-) (29) $) | (^ ([1][89][2468][048]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][2468][048]) (-) (0?2) (-) (29) $) | (^ ([1][89][13579][26]) (-) (0?2) (-) (29) $) | (^ ([2-9][0-9][13579][26]) (-) (0?2) (-) (29) $)) /);
if (result==null) {
Return "No";
}
return "yes";
}

This method is consistent with the above effect
function IsDate2 (DATESTR) {
var result = Datestr.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2});
if (result = = null)
Return "No";
var d = new Date (result[1], result[3]-1, result[4]);
if ((d.getfullyear () = = Result[1] && (D.getmonth () + 1) = = Result[3] && d.getdate () = = Result[4]) {
return "yes";
}
Return "No";
}

Determine if the character entered is Chinese
function Ischinese (str) {
if (str.length!=0) {
reg=/^[\u0391-\uffe5]+$/;
if (!reg.test (str)) {
Alert ("Sorry, the string type you typed is not well-formed!");
Return "No";
}
}
return "yes";
}


To determine if it is empty
function IsEmpty (str) {
if (str==null| | typeof str== "Undefined" | | Str.trim () = = "") {
return true;
}else{
return false;
}
}

Fixed telephone
function Testtelephone (phone) {
var phone_reg = new RegExp (/^ ([+]{0,1}\d{3,4}|\d{3,4}-)? \d{7,8}$/);
if (!phone_reg.test (phone)) {
Return "No";
}
return "yes";
}
Discount
function Isdiscount (discount) {
var phone_reg = new RegExp (/^ (0) ([\.] \d{1,2}) |1|1.00|1.0) $/);
if (!phone_reg.test (discount)) {
Return "No";
}
return "yes";
}
Mobile phone number
function Testmobile (MOBILE) {
var mobile_reg = new RegExp (/^0{0,1}1[0-9]{10}$/);
if (!mobile_reg.test (mobile)) {
Return "No";
}
return "yes";
}
QQ number starting from 10000
function Testqq (QQ) {
var qq_reg = new RegExp (/^[1-9][0-9]{4,}$/);
if (!qq_reg.test (QQ)) {
Return "No";
}
return "yes";
}
Email
function Testemail (email) {
var email_reg = new RegExp (/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *.\w+ ([-.] \w+) *$/);
if (!email_reg.test (email)) {
Return "No";
}
return "yes";
}

Positive integers with no symbols
function Testplusdigit (digit) {
var plusdigit_reg = new RegExp (/^\d+$/);
if (!plusdigit_reg.test (digit)) {
Return "No";
}
return "yes";
}

Double Price
function Testpriceformat (str) {
var priceformatreg = new RegExp (/^\d+ (. \d{1,2})? $/);
if (!priceformatreg.test (str)) {
Return "No";
}
return "yes";
}

Id
function Testidcard (str) {
var idcardreg = new RegExp (/(^\d{15}$) | ( ^\D{17} ([0-9]| X) $)/);
if (! Idcardreg.test (str)) {
Return "No";
}
return "yes";
}

2012-06-19 Date format
function TestDate (str) {
var datereg = new RegExp (/(^\d{4}-[0,1][0-9]-[0-3][0-9]$)/);
if (!datereg.test (str)) {
Return "No";
}
return "yes";
}



Floating-point precision operations (addition)
function Accadd (ARG1,ARG2) {
var r1,r2,m,n;
Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0}
Try{r2=arg2.tostring (). Split (".") [1].length}catch (e) {r2=0}
M=math.pow (10,math.max (R1,R2));
N= (R1>=R2) r1:r2;
Return ((arg1*m+arg2*m)/m). ToFixed (n);
}
Number.prototype.add = function (ARG) {
Return Accadd (Arg,this);
}

Floating-point precision operations (subtraction)
function Accsub (ARG1,ARG2) {
Return Accadd (ARG1,-ARG2);
}
Number.prototype.subtract = function (ARG) {
Return Accsub (THIS,ARG);
}

Floating-point precision operations (multiplication)
function Accmul (ARG1,ARG2)
{
var m=0,s1=arg1.tostring (), s2=arg2.tostring ();
Try{m+=s1.split (".") [1].length}catch (e) {}
Try{m+=s2.split (".") [1].length}catch (e) {}
Return number (S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m)
}
Number.prototype.mul = function (ARG) {
Return Accmul (ARG, this);
}

Floating-point precision Operations (division)
function Accdiv (ARG1,ARG2) {
var t1=0,t2=0,r1,r2;
Try{t1=arg1.tostring (). Split (".") [1].length}catch (e) {}
Try{t2=arg2.tostring (). Split (".") [1].length}catch (e) {}
With (Math) {
R1=number (Arg1.tostring (). Replace (".", ""))
R2=number (Arg2.tostring (). Replace (".", ""))
Return (R1/R2) *pow (10,T2-T1);
}
}
Number.prototype.div = function (ARG) {
Return Accdiv (this, ARG);
}

Limit input Numbers
function Isnumber (e) {
if ($.browser.msie) {
if ((Event.keycode >) && (Event.keycode < 58) | |
(Event.keycode = 8)) {
return true;
} else {
return false;
}
} else {
if ((E.which >) && (E.which < 58) | |
(E.which = 8)) {
return true;
} else {
return false;
}
}
}


String length interception
function Cutstr (str, len) {
var temp;
var icount = 0;
var patrn =/[^\x00-\xff]/;
var Strre = "";
for (var i = 0; i < str.length; i++) {
if (Icount < len-1) {
temp = Str.substr (i, 1);
if (patrn.exec (temp) = null) {
icount = icount + 1;
} else {
icount = icount + 2;
}
Strre + temp;
} else {
Break
}
}
return Strre + "...";
}

Get the domain name Master
function GetHost (URL) {
var host = "NULL";
if (typeof url = = "undefined" | | | null = = URL) {
url = window.location.href;
}
var regex =/^\w+\:\/\/([^\/]*). * *;
var match = Url.match (regex);
if (typeof match!= "undefined" && null!= match) {
host = match[1];
}
return host;
}

Determine if a value is in range
Rang=1 represents a positive integer [0,2147483647] 2 for Float[0,3.4028235e38]
return= ' empty ' means null input,
function Israng (Str,rang) {
if (typeof str = = "number") {
var num = number (str);
Determines whether a positive integer range
if (rang = = 1) {
if (testplusdigit (num) = "yes") {
if (num>=0&&num<=2147483647) {
return "Is_int";
}else{
return "Is_not_int_rang";
}
}else{
return "Is_not_int";
}
}else if (rang = = 2) {
if (Testpriceformat (num) = "yes") {
if (num>=0&&num<=3.4028235e38) {
return "Is_float";
}else{
return "Is_not_float_rang";
}
}else{
return "Is_not_float";
}
}else{
return "Rang_is_not_right";
}
}else{
return "Is_not_number";
}
}

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.