JS Common Custom Common Function summary

Source: Internet
Author: User
Tags pow split tostring trim

  This article has sorted out some JS common custom public functions such as checking whether the date format, judging whether the characters entered are Chinese, judging whether it is empty, fixed telephone, mobile phone number and so on  

The code is as follows: String.prototype.trim = function () {  return This.replace (/(^s*) | ( s*$)/g, ""); }   //check to see if the 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] && Amp D.getdate () = = Result[4]) {  return "yes"; }  return "no"; }   //Determine if the character entered is Chinese   func tion 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"; }     //determine if NULL   function IsEmpty (str) {& nbsp if (str==null| | typeof str== "Undefined" | | Str.trim () = "") {  return true; }else{  return false; } }   //fixed telephone   function t Esttelephone (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 m Obile_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"; } //e-mail   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 number exact operation (addition)   function Accadd (a RG1,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); &nbSp }  Number.prototype.subtract = function (ARG) {  return accsub (this,arg); }   //floating-point precision operations (multiplication) & nbsp 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)   FU Nction 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); } }  = function (ARG) {  return Accdiv (this, arg); }   //Limit input number   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; } } }    &NBSP ; String length interception   function cutstr (str, len) {  var temp;  var icount = 0;  var patrn =/[^x00-xff]/;  va R 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  Sp }  return Strre + "..."; }   //Get domain host   function gethost (URL) {  var host = "Null";  I F (typeof url = = "undefined" | | | null = = URL) {  urL = window.location.href; }  var regex =/^w+://([^/]*) .*/;  var match = Url.match (regex);  if (typeo F match!= "undefined" && null!= match) {  host = match[1]; }  return host; }    Determines whether a value is in the range  //rang=1 represents a positive integer [0,2147483647] 2 indicates that the float[0,3.4028235e38] //return= ' empty ' represents an empty input,  function Israng (Str,rang) {  if (typeof str = "number") {  var num = number (str); //Determine if the positive integer range   if (ran G = = 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"; }&nbsp }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.