Below are some JS verifications and functions I often use. Some verifications I have written directly into the properties of the object, and can be called directly through the object.
The code copy is as follows:
//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(10, t2 - t1)).toFixed(n);
}
}
The code copy is as follows:
//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(10, m)).toFixed(n);
}
The code copy is as follows:
//Floating point number addition operation
function faadd(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(10, Math.max(r1, r2))
return ((a * m + b * m) / m).toFixed(n);
}
The code copy is as follows:
//Floating point number 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(10, 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);
}
The code copy is as follows:
///Format the number of digits, the number of insufficient digits is 0 by default. If parameter 2 is specified and the value of parameter 2 is 1, the right side will be 0 by 0 if parameter 2 is specified.
Number.prototype.FormatLen = function (len, direct) {
var d = parseInt(direct);
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;
}
The code copy is as follows:
//Format the decimal places, you can specify the number of decimal places, whether to round or not and other parameters such as
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;
}
The code copy is as follows:
// Detect whether the same element in the string split is a string separator. If a separator is specified, it is determined whether there is duplication of the string delimiter. If it is not specified, it is determined whether there is duplication of a single string.
//Return true if there is a duplicate
String.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;
}
The code copy is as follows:
//The string is converted into the number of groups. If the delimiter s is not specified, the delimiter is delimited by default. If the delimiter is empty, each character is used as an array element.
String.prototype.ToArray = function (s) {
if (s == undefined) { s = ","; }
var strArr = [];
strArr = this.split(s);
return strArr;
}
The code copy is as follows:
//Convert an array to a string, all elements are connected using the specified delimiter, and the default is,
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;
}
The code copy is as follows:
//Get the location index of the specified element, the element does not exist and returns -1
Array.prototype.GetIndex = function (s) {
var index = -1;
for (var i = 0; i < this.length; i++) {
if ((s + "") == this[i]) {
index = i;
}
}
return index;
}
The code copy is as follows:
//Remove 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();
}
The code copy is as follows:
///Sort the array numerically asc specifies whether to sort asc. It can be true or false, not specified as ascending order.
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); }
//SortByNumer to sort numbers in descending order in array
function SortNumberDesc(a, b) {
return b - a;
}
//SortByNumer to sort numbers in ascending order in arrays with Array.SortByNumer
function SortNumberAsc(a, b) {
return a - b;
}
//This is an independent function
function LTrim(str, s) {
if (s == undefined) { s = " "; }
if (str == s && s != " ") { return s; }
var i;
for (i = 0; i < 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);
}
The code copy is as follows:
///Detection whether the string is composed of Chinese, English, numbers and underscores
function chkNickName(str) {
var pattern = /^[/w/u4e00-/u9fa5]+$/gi;
if (pattern.test(str)) {
return true;
}
return false;
}
The code copy is as follows:
//Judge length (length is not limited to 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 && calculated_byte(this) <= maxnum ? true : false));
return isRightFormat;
}
The code copy is as follows:
//Verify whether the string is a letter + number +_+-
String.prototype.IsStr = function () {
var myReg = /^[0-9a-zA-Z/-/_]+$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify username
String.prototype.IsUsername = function () {
var myReg = /^[0-9a-zA-Z/-/_]{3,50}$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify password
String.prototype.IsPassword = function () {
var myReg = /^[0-9a-zA-Z`~!@#$%^&*()-_+=/{/}/[/]/;/:/"/'/?////]{6,}$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify whether it is a letter
String.prototype.IsEn = function () {
var myReg = /^[a-zA-Z]+$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify whether it is Chinese characters
String.prototype.IsCn = function () {
var myReg = /^[/u0391-/uFFE5]+$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify E-mail
String.prototype.IsEmail = function () {
var myReg = /([/w-/.]+)@((/[[0-9]{1,3}/.[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;
}
The code copy is as follows:
//Verify MSN
String.prototype.IsMSN = function () {
var myReg = /([/w-/.]+)@((/[[0-9]{1,3}/.[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;
}
The code copy is as follows:
//Verify QQ number
String.prototype.IsQQ = function () {
var myReg = /^[1-9]/d{4,10}$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify the URL
String.prototype.IsHttpUrl = function () {
var myReg = /^http:////[A-Za-z0-9]+/.[A-Za-z0-9]+[//=/?%/-&_~`@[/]/':+!]*([^<>/"/"])*$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify 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;
}
The code copy is as follows:
//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]{1,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2})$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify the image address (images generated dynamically by CGI are not supported)
String.prototype.IsImgURL = function () {
var myReg = /^/.(jpeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify mobile phone number
String.prototype.IsCellPhone = function () {
var myReg = /^((/(/d{3}/))|(/d{3}/-))?1[3,5]/d{9}$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify landline phone
String.prototype.IsPhone = function () {
var myReg = /^[+]{0,1}(/d){1,3}[ ]?([-]?(((/d)|[ ]){1,12})+$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify the zip code
String.prototype.IsZipCode = function () {
var myReg = /[0-9]{6}/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//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;
}
The code copy is as follows:
//Verify date format YY-MM-DD
String.prototype.IsDateFormat = function () {
var myReg = /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify time format HH:MM:SS
String.prototype.IsRangeTime = function () {
var myReg = /^(/d{2}):(/d{2}):(/d{2})$/;
if (myReg.test(this)) return true;
return false;
}
The code copy is as follows:
//Verify the 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 code copy is as follows:
// Words verify the number format and judge the number's surroundings (min: minimum value; max: maximum value.)
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 && this < maxnum ? true : false));
}
return isRightFormat;
}
The code copy is as follows:
//Verify search keywords
String.prototype.IsSearch = function () {
var myReg = /^[/|/"/'<>,.*&@#$;:!^()]/;
if (myReg.test(this)) return false;
return true;
}
The code copy is as follows:
//js accurately calculates 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;
}
The code copy is as follows:
//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;
}
The code copy is as follows:
//js date formatting
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.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(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] : ("00" + 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));
}
The code copy is as follows:
//Is 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;
}
The code copy is as follows:
//Get the object triggered by the event
function getEventTarget(e) {
e = e || window.event;
return e.target || e.srcElement;
}
The code is simple, but the functions are very practical. Please refer to it if you need it.