Common udfs in js _ basic knowledge-js tutorial

Source: Internet
Author: User
This article has sorted out some common js custom public functions, such as checking whether the date format is used, determining whether the input characters are Chinese, determining whether the input characters are empty, landline phone number, and mobile phone number. The Code is as follows:


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

// Check whether the date format is used
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? [2, 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 has the same effect as above
Function isDate2 (datestr ){
Var result = datestr. match (/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d {}) $ /);
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 whether the input character is Chinese
Function IsChinese (str ){
If (str. length! = 0 ){
Reg =/^ [\ u0391-\ uFFE5] + $ /;
If (! Reg. test (str )){
// Alert ("sorry, the format of the string type you entered is incorrect! ");
Return "no ";
}
}
Return "yes ";
}


// Judge whether it is null
Function isEmpty (str ){
If (str = null | typeof str = "undefined" | str. trim () = ""){
Return true;
} Else {
Return false;
}
}

// Fixed phone number
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.00}) | 1 | 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 starts 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 ";
}

// Unsigned positive integer
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 card
Function testIDCard (str ){
Var IDCardReg = new RegExp (/(^ \ d {15} $) | (^ \ d {17} ([0-9] | X) $ )/);
If (! IDCardReg. test (str )){
Return "no ";
}
Return "yes ";
}

// 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 ";
}



// Exact floating-point calculation (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 );
}

// Exact floating-point calculation (subtraction)
Function accSub (arg1, arg2 ){
Return accAdd (arg1,-arg2 );
}
Number. prototype. subtract = function (arg ){
Return accSub (this, arg );
}

// Exact floating-point calculation (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 );
}

// Exact floating-point calculation (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. p = function (arg ){
Return accDiv (this, arg );
}

// Restrict the input number
Function isNumber (e ){
If ($. browser. msie ){
If (event. keyCode> 47) & (event. keyCode <58) |
(Event. keyCode = 8 )){
Return true;
} Else {
Return false;
}
} Else {
If (e. which> 47) & (e. which <58) |
(E. which = 8 )){
Return true;
} Else {
Return false;
}
}
}


// String Length Truncation
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.exe c (temp) = null ){
Icount = icount + 1;
} Else {
Icount = icount + 2;
}
Strre + = temp;
} Else {
Break
}
}
Return strre + "...";
}

// Obtain the domain name host
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 whether a value is in the specified range
// Rang = 1 indicates a positive integer [0, 2147483647] 2 indicates float [0, 3.4028235E38]
// Return = 'empty' indicates that the input is empty,
Function isRang (str, rang ){
If (typeof str = "number "){
Var num = Number (str );
// Determine whether the value is in the 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.