Here are some of my usual JS validation and functions, there are some validation I write directly to the object's properties, you can directly through the object. method to invoke
Floating-point division operation function Fdiv (A, B, n) {if (n = = undefined) {n = 2;} var t1 = 0, t2 = 0, R1, R2; try {t1 = a.tostring (). Split (".") [1].length} catch (e) {} try {t2 = b.tostring (). Split (".") [1].length} catch (e) {} with (Math) {r1 = number (a.tostring (). Replace (".", "")); r2 = number (b.tostring (). Replace (".", "")); Return ((R1/R2) * POW (T2-T1)). toFixed (n); }}//floating-point multiplication operation function Fmul (A, B, n) {if (n = = undefined) {n = 2;} var m = 0, S1 = a.tostring (), S2 = b.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 (ten, M)). toFixed (n);} Floating-point addition operation function Fadd (A, B, n) {if (n = = undefined) {n = 2;} var r1, R2, M; try {r1 = a.tostring (). Split (".") [1].length} catch (e) {r1 = 0} try {r2 = b.tostring (). Split (".") [1].length} catch (e) {r2 = 0} m = Math.pow (Math.max (R1, R2)) return ((A * m + b * m)/m). ToFixed (n);} Floating-point subtraction operation function Fsub (A, B, n) {if (n = = undefined) {n = 2;} var r1, R2, M; try {r1 = a.tostring (). Split (".") [1].length} catch (e) {r1 = 0} try {r2 = b.tostring (). Split (".") [1].length} catch (e) {r2 = 0} m = Math.pow (Ten, Math.max (R1, r2)); Dynamic control accuracy Length//n = (R1 >= r2)? R1:R2; Return ((A * m-b * m)/m). ToFixed (n);} Number.prototype.add = function (ARG) {return Fadd (this, arg);} Number.prototype.subs = function (ARG) {return fsub (this, arg);} Number.prototype.mul = function (ARG) {return Fmul (this, arg);} Number.prototype.div = function (ARG) {return fdiv (this, arg);} Format number of digits, default left 0, if parameter 2 is specified and the value of parameter 2 is 1, the right complement 0number.prototype.formatlen = function (len, direct) {var d = parseint (dire CT); if (IsNaN (d)) {d = 0;} var num = this.tostring (); if (Num.length < Len) {for (var i = num.length; i < Len; i++) {if (d = = 0) {num = "0" + num; } else {num + = "0"; }}} return num;} Format decimal digits, you can specify the number of decimals, whether rounding parameters Number.prototype.FormatRadix = function (len, isround) {var num = this.tostring (); var Numarr = Num.split ('. '); var rad = 0; var Numpart = parseint (numarr[0]); if (numarr.length >= 2) {if (Numarr[1].length < len) {rad = parseint (numarr[1]). Formatlen (len, 1); } else {if (numarr[1].length = = len) {rad = numarr[1]; } else {rad = numarr[1].substr (0, Len); if (Isround) {var d = parseint (Numarr[1].substr (len, 1)); if (d >= 5) {rad + 1, if (rad.tostring () length > Len) {numpart + 1; rad = Rad.tostring (). substr (1, Len);}} }}}} else {rad = rad. Formatlen (len); } return Numpart + "." + rad;} Detects if the same element in the string split is a string delimiter, and if a delimiter is specified, the delimitedThe delimiter is delimited if the string is duplicated, if not specified, to determine whether a single string has duplicate//duplicate return trueString.prototype.CompareElement = function (s) {var str = this.tostring (); if (s = = undefined) {for (var i = 0; i < str.length; i++) {for (j = i + 1; j < Str.length; j + +) {if (Str.substr (i, 1) = = Str.substr (j, 1)) {return true; }}}} else {var Strarr = Str.split (s); for (var i = 0, i < strarr.length; i++) {for (var j = i + 1; j < Strarr.length; J + +) {if (Strarr[i] = = Strarr[j]) {return true; }}}} return false;} STRING.PROTOTYPE.REPLACEALL = function (str, tostr) {ostr = this; while (Ostr.indexof (str) >-1) {ostr = Ostr.replace (str, TOSTR); } return ostr;} Array.prototype.CompareElement = function () {var strarr = this; for (var i = 0, i < strarr.length; i++) {for (var j = i + 1; J < STrarr.length; J + +) {if (strarr[i] = = Strarr[j]) {return true; }}} return false;} String to the number of groups, if the delimiter s is not specified, the default is to delimit the delimiter, if the specified delimiter is empty, each character as an array element String.prototype.ToArray = function (s) {if (s = = undefined) {s = ","; } var strarr = []; Strarr = This.split (s); return Strarr;} Converts an array to a string, all elements are concatenated using the specified delimiter, separated by default, Array.prototype.ToIDList = function (s) {if (s = = undefined) {s = ",";} var list = ""; for (var i = 0; i < this.length; i++) {list = = (List = =)? This[i]: S + "" + this[i]); } return list;} Gets the position index of the specified element, the element does not exist return -1array.prototype.getindex = function (s) {var index =-1; for (var i = 0; i < this.length; i++) {if ((S + "") = = This[i]) {index = i; }} return index;} Removes the specified element from the array Array.prototype.Remove = function (s) {var list = ""; for (var i = 0; i < this.length; i++) {if (s! = This[i]) {list + = (List = =)? This[i]: "," + thiS[i]); }} return list. ToArray ();} Sort the array numerically ASC specifies whether to sort in ascending order, either true or FALSE, not specified as ascending Array.prototype.SortByNumber = function (ASC) {if (ASC = = undefined) { ASC = TRUE; } if (ASC) {return this.sort (SORTNUMBERASC); } else {return this.sort (SORTNUMBERDESC); }}array.prototype.inarray = function (e) {var IsIn = false; for (var i = 0; i < this.length; i++) {if (this[i] = = (E + "")) {IsIn = true; }} return IsIn;} String.prototype.Trim = function (s) {return Trim (this, s);} String.prototype.LTrim = function (s) {return LTrim (this, s);} String.prototype.RTrim = function (s) {return RTrim (this, s);} With Array.sortbynumer used, the numbers are sorted in descending order of function Sortnumberdesc (A, b) {return b-a;} With Array.sortbynumer used, the numbers are sorted in ascending order function Sortnumberasc (A, b) {return a-B;} Here is the standalone function LTrim (str, s) {if (s = = undefined) {s = "";} if (str = = S && s! = "") {return s;} var i; for (i = 0; I &Lt Str.length; i++) {if (Str.charat (i)! = s && str.charat (i)! = s) break; } str = str.substring (i, str.length); return str;} function RTrim (str, s) {var i; if (str = = S && s! = "") {return s;} if (s = = undefined) {s = "";} for (i = str.length-1; I >= 0; i--) {if (Str.charat (i)! = s && str.charat (i)! = s) break; } str = str.substring (0, i + 1); return str;} function Trim (str, s) {return LTrim (RTrim (str, s), s);} Detects whether a string is composed of Chinese, English, numeric, and underscore function chknickname (str) {var pattern =/^[\w\u4e00-\u9fa5]+$/gi; if (Pattern.test (str)) {return true; } return false;} Judging length (unlimited length is 0) String.prototype.IsLen = function () {var isrightformat = false; var minnum = arguments[0]? Arguments[0]: 0; var maxnum = arguments[1]? ARGUMENTS[1]: 0; Isrightformat = (Minnum = = 0 && Maxnum = = 0? True: (Calculate_byte (this) >= minnum && calculate_byte ( This) <= Maxnum? True: false)); return Isrightformat;} Verify that the string is a letter + number +_+-STRING.PROTOTYPE.ISSTR = function () {var myreg =/^[0-9a-za-z\-\_]+$/; if (Myreg.test (this)) return true; return false;} Verify user Name String.prototype.IsUsername = function () {var myreg =/^[0-9a-za-z\-\_]{3,50}$/; if (Myreg.test (this)) return true; return false;} Verify Password String.prototype.IsPassword = function () {var myreg =/^[0-9a-za-z ' [email protected]#$%^&* ()-_+=\{\}\ [\]\;\:\ "\ ' \?\/\\]{6,}$/; if (Myreg.test (this)) return true; return false;} Verify that the letter String.prototype.IsEn = function () {var myreg =/^[a-za-z]+$/; if (Myreg.test (this)) return true; return false;} Verify that the kanji String.prototype.IsCn = function () {var myreg =/^[\u0391-\uffe5]+$/; if (Myreg.test (this)) return true; return false;} Verify E-mailstring.prototype.isemail = function () {var myreg =/([\w-\.] +) @ ((\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | ([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) /; if (Myreg.test (this))return true; return false;} Verify MSNString.prototype.IsMSN = function () {var myreg =/([\w-\.] +) @ ((\[[0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\.) | ([\w-]+\.) +)) ([a-za-z]{2,4}| [0-9] {1,3}) (\]?) /; if (Myreg.test (this)) return true; return false;} Verify QQ Number String.prototype.IsQQ = function () {var myreg =/^[1-9]\d{4,10}$/; if (Myreg.test (this)) return true; return false;} Verify URL String.prototype.IsHttpUrl = function () {var myreg =/^http:\/\/[a-za-z0-9]+\.[ a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "\"]) *$/; if (Myreg.test (this)) return true; return false;} Verify that the domain name String.prototype.IsDoMainName = function () {var myreg =/^[0-9a-za-z] ([0-9a-za-z\-]+\.) {1,3} [A-za-z] {2,4}?$/; if (Myreg.test (this)) return true; return false;} Verify IPV4 Address String.prototype.IsIpv4 = function () {var myreg =/^ (2[0-5]{2}|1?[ 0-9]{1,2}). (2[0-5]{2}|1?) [0-9] {.}). (2[0-5]{2}|1?) [0-9] {.}). (2[0-5]{2}|1?) [0-9] {.}) $/; if (Myreg.test (this)) return true; return false;} Verify the image address (notSupports dynamically generated images by CGI) String.prototype.IsImgURL = function () {var myreg =/^\. JPEG|JPG|GIF|BMP|PNG|PCX|TIFF|TGA|LWF) $/; if (Myreg.test (this)) return true; return false;} Verify mobile Number String.prototype.IsCellPhone = function () {var myreg =/^ ((\ (\d{3}\)) | ( \d{3}\-))? 1[3,5]\d{9}$/; if (Myreg.test (this)) return true; return false;} Verify that the fixed phone String.prototype.IsPhone = function () {var myreg =/^[+]{0,1} (\d) {1,3}[]? ( [-]? ((\d) | []) {1,12}) +$/; if (Myreg.test (this)) return true; return false;} Verify Zip String.prototype.IsZipCode = function () {var myreg =/[0-9]{6}/; if (Myreg.test (this)) return true; return false;} Verify the ID number String.prototype.IsIdCard = function () {var myreg =/(^ ([\d]{15}|[ \d]{18}| [\d] {17} [XX] {1}) $)/; if (Myreg.test (this)) return true; return false;} Verify date format Yy-mm-ddstring.prototype.isdateformat = function () {var myreg =/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/; if (Myreg.test (this)) return true; return false;} Verify the time format HH:MM:SSString.prototypE.israngetime = function () {var myreg =/^ (\d{2}):(\d{2}):(\d{2}) $/; if (Myreg.test (this)) return true; return false;} Verify amount Format String.prototype.IsMoney = function () {var myreg =/^[0-9]{1,8}[.] {0,1} [0-9] {0,6}$/; if (Myreg.test (this)) return true; return false;} The word verifies the number format and determines the circumference of the number (min: minimum; Max: max.) String.prototype.IsInt = function () {var isrightformat = false; var minnum = arguments[0]? Arguments[0]: 0; var maxnum = arguments[1]? ARGUMENTS[1]: 0; var myreg =/^[-\+]?\d+$/; if (Myreg.test (this)) {Isrightformat = (Minnum = = 0 && Maxnum = = 0) True: (This > Minnum && t His < Maxnum? True:false)); } return Isrightformat;} Verify search Keyword String.prototype.IsSearch = function () {var myreg =/^[\|\ "\ ' <>,.*&@#$;:!^ ()]/; if (Myreg.test (this)) return false; return true;} JS accurately calculates the string length function Calculate_byte (stargetstr) {var stmpstr, Stmpchar; var noriginlen = 0; var nstrlength = 0; STMPSTR = new String (STARGETSTR); Noriginlen = Stmpstr.length; for (var i = 0; i < Noriginlen; i++) {Stmpchar = Stmpstr.charat (i); if (Escape (Stmpchar). Length > 4) {nstrlength + = 2; } else if (stmpchar! = ' \ r ') {nstrlength++; }} return nstrlength;} color value; String.prototype.IsColor = function () {var s = arguments[0]? Arguments[0]: ""; s = S.trim (); if (s.length! = 7) return false; Return S.search (/\#[a-fa-f0-9]{6}/)! =-1;} JS Date Format Date.prototype.format = function (format) {var o = {"m+": This.getmonth () + 1,//month "d+": This.getdate (),//day "H +": this.gethours (),//hour "m+": This.getminutes (),//minute "s+": THIS.G Etseconds (),//second "q+": Math.floor ((This.getmonth () + 3)/3),//quarter "S": this.getmilliseconds ()/ /millisecond} if (/(y+)/.test (format)) {format = Format.replace (regexp.$1, (this.getfullyear () + ""). Subst R (4-regexp.$1.length)); } for (var k in O) {if (New RegExp ("(" + K + ")"). Test (format)) {format = Format.replace (regexp.$1, Regexp.$1.length = = 1? O[k]: ("XX" + o[k]). substr (("" + o[k]). length); }} return format;} function Haschinese (value) {if (/^[\u4e00-\u9fa5]+$/.test (value)) {return true; } return false;} function ToDate (datestr) {var dstr = datestr.tostring (); Datestr = Dstr.replaceall ("-", "/"); return new Date (Date.parse (DATESTR));} Whether the id list String.prototype.IsIdList = function (s) {if (s = = undefined) {s = ","; } var arr = This.split (s); for (var i = 0; i < arr.length; i++) {if (IsNaN (parseint (arr[i))) {return false;} } return true; Gets the event-triggered object function Geteventtarget (e) {e = e | | window.event; return E.target | | E.srcelement;}
Share some of my usual JS validations and functions