Voici quelques vérifications et fonctions JS que j'utilise souvent. Certaines vérifications que j'ai écrites directement dans les propriétés de l'objet et peuvent être appelées directement via l'objet.
La copie de code est la suivante:
// Opération de division des points flottants
fonction fdiv (a, b, n) {
if (n == Undefined) {n = 2; }
var t1 = 0, t2 = 0, r1, r2;
essayez {t1 = a.toString (). Split (".") [1] .length} catch (e) {}
try {t2 = b.toString (). Split (".") [1] .length} catch (e) {}
avec (math) {
r1 = nombre (a.toString (). Remplace (".", ""));
r2 = nombre (b.toString (). Remplace (".", ""));
return ((r1 / r2) * pow (10, t2 - t1)). tofixed (n);
}
}
La copie de code est la suivante:
// Opération de multiplication des points flottants
fonction 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) {}
essayez {m + = s2.split (".") [1] .length} catch (e) {}
return (nombre (s1.replace (".", "") * numéro (s2.replace (".", "") / math.pow (10, m)). tofixed (n);
}
La copie de code est la suivante:
// Opération d'ajout de numéros de point flottante
fonction faadd (a, b, n) {
if (n == Undefined) {n = 2; }
var r1, r2, m;
try {r1 = a.toString (). Split (".") [1] .length} catch (e) {r1 = 0}
essayez {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);
}
La copie de code est la suivante:
// Numéro de point flottante Opération de soustraction
fonction fsub (a, b, n) {
if (n == Undefined) {n = 2; }
var r1, r2, m;
try {r1 = a.toString (). Split (".") [1] .length} catch (e) {r1 = 0}
essayez {r2 = b.toString (). Split (".") [1] .length} catch (e) {r2 = 0}
m = math.pow (10, math.max (r1, r2));
// longueur de précision de contrôle dynamique
// n = (r1> = r2)? R1: R2;
return ((a * m - b * m) / m) .tofixed (n);
}
Nombre.prototype.add = fonction (arg) {
retour fadd (ceci, arg);
}
Nombre.prototype.subs = fonction (arg) {
retourner fsub (ceci, arg);
}
Nombre.prototype.mul = fonction (arg) {
retourner fmul (ceci, arg);
}
Nombre.prototype.div = fonction (arg) {
retourner fdiv (ceci, arg);
}
La copie de code est la suivante:
/// Formater le nombre de chiffres, le nombre de chiffres insuffisants est de 0 par défaut. Si le paramètre 2 est spécifié et que la valeur du paramètre 2 est 1, le côté droit sera 0 par 0 si le paramètre 2 est spécifié.
Number.prototype.formatlen = fonction (len, Direct) {
var d = parseInt (direct);
if (isnan (d)) {d = 0; }
var num = this.toString ();
if (num.length <len) {
pour (var i = num.length; i <len; i ++) {
if (d == 0) {
num = "0" + num;
}
autre {
num + = "0";
}
}
}
retour num;
}
La copie de code est la suivante:
// format les décimales, vous pouvez spécifier le nombre de décimales, que ce soit pour arrondir ou non et d'autres paramètres tels que
Nombre.prototype.formatradix = fonction (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);
}
autre {
if (numarr [1] .length == len) {
rad = numarr [1];
}
autre {
rad = numarr [1] .substr (0, len);
if (isround) {
var d = parseInt (numarr [1] .substr (len, 1));
if (d> = 5) {rad + = 1; if (rad.toString (). longueur> len) {numpart + = 1; rad = rad.toString (). substr (1, len); }}
}
}
}
}
autre {
rad = rad.formatlen (len);
}
Retour Numpart + "." + rad;
}
La copie de code est la suivante:
// Détection si le même élément dans la fraction de chaîne est un séparateur de chaîne. Si un délimiteur est spécifié, il est déterminé s'il y a la duplication du délimiteur de chaîne. S'il n'est pas spécifié, il est déterminé s'il y a la duplication d'une seule chaîne.
// retourne vrai s'il y a un double
String.prototype.compareelement = function (s) {
var str = this.toString ();
if (s == Undefined) {
pour (var i = 0; i <str.length; i ++) {
pour (j = i + 1; j <str.length; j ++) {
if (str.substr (i, 1) == str.substr (j, 1)) {
Retour Vrai;
}
}
}
}
autre {
var starr = str.split (s);
pour (var i = 0; i <starr.length; i ++) {
pour (var j = i + 1; j <starr.length; j ++) {
if (Strarr [i] == Strarr [J]) {
Retour Vrai;
}
}
}
}
retourne false;
}
String.prototype.replaceALL = fonction (str, tostr) {
ostr = this;
while (ostr.indexof (str)> -1) {
ostr = ostr.replace (str, tostr);
}
retour OSTR;
}
Array.prototype.compareelement = function () {
var starr = this;
pour (var i = 0; i <starr.length; i ++) {
pour (var j = i + 1; j <starr.length; j ++) {
if (Strarr [i] == Strarr [J]) {
Retour Vrai;
}
}
}
retourne false;
}
La copie de code est la suivante:
// La chaîne est convertie en nombre de groupes. Si le délimiteur n'est pas spécifié, le délimiteur est délimité par défaut. Si le délimiteur est vide, chaque caractère est utilisé comme élément de tableau.
String.prototype.toarray = fonction (s) {
if (s == Undefined) {s = ","; }
var starr = [];
strarr = this.split (s);
retour Strarr;
}
La copie de code est la suivante:
// Convertir un tableau en une chaîne, tous les éléments sont connectés à l'aide du délimiteur spécifié, et la valeur par défaut est,
Array.prototype.toidlist = fonction (s) {
if (s == Undefined) {s = ","; }
var list = "";
for (var i = 0; i <this.length; i ++) {
list + = (list == ""? this [i]: s + "" + this [i]);
}
Liste de retour;
}
La copie de code est la suivante:
// obtient l'indice de localisation de l'élément spécifié, l'élément n'existe pas et renvoie -1
Array.prototype.getIndex = fonction (s) {
var index = -1;
for (var i = 0; i <this.length; i ++) {
if ((s + "") == this [i]) {
index = i;
}
}
Index de retour;
}
La copie de code est la suivante:
// Retirez l'élément spécifié du tableau
Array.prototype.remove = fonction (s) {
var list = "";
for (var i = 0; i <this.length; i ++) {
if (s! = this [i]) {
list + = (list == ""? this [i]: "," + this [i]);
}
}
return list.toArray ();
}
La copie de code est la suivante:
/// Trier le tableau ASC numériquement spécifie s'il faut trier ASC. Il peut être vrai ou faux, non spécifié comme ordre croissant.
Array.prototype.sortbynumber = fonction (asc) {
if (asc == Undefined) {asc = true; }
if (asc) {
Renvoyez ceci.sort (SortNumberasc);
}
autre {
Renvoie ceci.sort (SortNumberDesc);
}
}
Array.prototype.inarray = fonction (e) {
var isin = false;
for (var i = 0; i <this.length; i ++) {
if (this [i] == (e + "")) {
Isin = true;
}
}
retour isin;
}
String.prototype.trim = fonction (s) {return Trim (this, s); }
String.prototype.ltrim = fonction (s) {return ltrim (this, s); }
String.prototype.rtrim = fonction (s) {return rtrim (this, s); }
// sortbynumer pour trier les nombres en ordre décroissant dans le tableau
fonction sortnumberdes (a, b) {
retour b - a;
}
// sortbynumer pour trier les nombres en ordre ascendant dans les tableaux avec array.sortbynumer
fonction sortnumberasc (a, b) {
retourner a - b;
}
// c'est une fonction indépendante
fonction ltrim (str, s) {
if (s == Undefined) {s = ""; }
if (str == s && s! = "") {return s; }
var i;
pour (i = 0; i <str.length; i ++) {
if (str.charat (i)! = s && str.charat (i)! = s) pause;
}
str = str.substring (i, str.length);
retour STR;
}
fonction rtrim (str, s) {
var i;
if (str == s && s! = "") {return s; }
if (s == Undefined) {s = ""; }
pour (i = str.length - 1; i> = 0; i--) {
if (str.charat (i)! = s && str.charat (i)! = s) pause;
}
str = str.substring (0, i + 1);
retour STR;
}
Fonction Trim (str, s) {
retour ltrim (rtrim (str, s), s);
}
La copie de code est la suivante:
/// Détection si la chaîne est composée de chinois, d'anglais, de chiffres et de soulignements
fonction chknickname (str) {
Var Pattern = / ^ [/ w / u4e00- / u9fa5] + $ / gi;
if (pattern.test (str)) {
Retour Vrai;
}
retourne false;
}
La copie de code est la suivante:
// Longueur du juge (la longueur n'est pas limitée à 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: (calcul_byte (this)> = minnum && calculate_byte (this) <= maxnum? true: false));
return IsRightFormat;
}
La copie de code est la suivante:
// Vérifiez si la chaîne est une lettre + numéro + _ + -
String.prototype.isstr = function () {
var myreg = / ^ [0-9a-za-z / - / _] + $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le nom d'utilisateur
String.prototype.issername = function () {
var myreg = / ^ [0-9a-za-z / - / _] {3,50} $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le mot de passe
String.prototype.ispassword = fonction () {
var myreg = / ^ [0-9a-za-z` ~! @ # $% ^ & * () -_ + = / {/} / [/] /; /: / "/ '/? ////] {6,} $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez si c'est une lettre
String.prototype.isen = function () {
var myreg = / ^ [a-za-z] + $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez si ce sont des caractères chinois
String.prototype.iscn = function () {
var myreg = / ^ [/ u0391- / uffe5] + $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le courrier électronique
String.prototype.isemail = function () {
var myreg = /(@/w-/.
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez MSN
String.prototype.ismsn = function () {
var myreg = /(@/w-/.
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le numéro QQ
String.prototype.isqq = function () {
var myreg = / ^ [1-9] / d {4,10} $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez l'URL
String.prototype.ishttpurl = fonction () {
var myreg = /^http:////[a-za-z0-9 diplômé/.
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le nom de domaine
String.prototype.isdomainName = function () {
var myreg = /^_0-9a-zA-Z
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez l'adresse IPv4
String.prototype.isipv4 = fonction () {
var myreg = /^(2@0-5
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez l'adresse d'image (les images générées dynamiquement par CGI ne sont pas prises en charge)
String.prototype.isimgurl = fonction () {
var myReg = /^/.(jPeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$/;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le numéro de téléphone mobile
String.prototype.iscellphone = function () {
var myreg = / ^ ((/ (/ d {3} /)) | (/ d {3} / -))? 1 [3,5] / d {9} $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le téléphone fixe
String.prototype.isphone = function () {
var myreg = / ^ [+] {0,1} (/ d) {1,3} []? ([-]? (((/ d) | []) {1,12}) + $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le code postal
String.prototype.iszipcode = function () {
var myreg = / [0-9] {6} /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le numéro d'identification
String.prototype.isidcard = function () {
var myreg = / (^ ([/ d] {15} | [/ d] {18} | [/ d] {17} [xx] {1}) $) /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le format de date yy-mm-dd
String.prototype.isdateformat = function () {
var myreg = / ^ [0-9] {4} - [0-9] {1,2} - [0-9] {1,2} $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le format de temps HH: MM: SS
String.prototype.isRangeTime = function () {
var myreg = / ^ (/ d {2}): (/ d {2}): (/ d {2}) $ /;
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Vérifiez le format du montant
String.prototype.ismoney = function () {
var myreg = /^[0-9
if (myreg.test (this)) renvoie true;
retourne false;
}
La copie de code est la suivante:
// Les mots vérifient le format de nombre et jugent la limite du nombre (min: valeur minimale; max: valeur maximale.)
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;
}
La copie de code est la suivante:
// Vérifiez les mots clés de recherche
String.prototype.issearch = function () {
var myReg = /^@/|/"/'<>,.*&@#$ ;:!^()
if (myreg.test (this)) renvoie false;
Retour Vrai;
}
La copie de code est la suivante:
// JS calcule avec précision la longueur des chaînes
fonction calcul_byte (stargetstr) {
var stmpstr, stmpchar;
var noriginlen = 0;
var nstrLength = 0;
stmptr = new String (StarGetstr);
noriginlen = stmptr.length;
pour (var i = 0; i <noriginlen; i ++) {
stmpchar = stmptr.charat (i);
if (Escape (stmpchar) .length> 4) {
nstrLength + = 2;
} else if (stmpchar! = '/ r') {
nstrLength ++;
}
}
return nstrLength;
}
La copie de code est la suivante:
// valeur de couleur;
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;
}
La copie de code est la suivante:
// Formatage de la date JS
Date.prototype.format = fonction (format) {
var o = {
"M +": this.getMonth () + 1, // mois
"d +": this.getDate (), // jour
"h +": this.gethours (), // heure
"M +": this.getMinutes (), // minute
"S +": this.getSeconds (), // seconde
"Q +": Math.floor ((this.getMonth () + 3) / 3), // Quarter
"S": this.getMilliseconds () // milliseconde
}
if (/(y+)/.test(format)) {
format = format.replace (regexp. 1 $, (this.getlyear () + "") .substr (4 - regexp. 1.Lengle));
}
pour (var k dans o) {
if (new regexp ("(" + k + ")"). test (format)) {
format = format.replace (regexp. 1 $, regexp. 1 1.Length == 1? O [k]: ("00" + o [k]). substr (("" + o [k]). longueur));
}
}
format de retour;
}
fonction Haschinese (valeur) {
if (/^@/u4e00-/u9fa5
Retour Vrai;
}
retourne false;
}
fonction todate (datestr) {
var dstr = datestr.toString ();
datestr = dstr.replaceAll ("-", "/");
return new Date (date.parse (datestr));
}
La copie de code est la suivante:
// est la liste des identifiants?
String.prototype.isidlist = fonction (s) {
if (s == Undefined) {
S = ",";
}
var arr = this.split (s);
pour (var i = 0; i <arr.length; i ++) {
if (isnan (parseInt (arr [i]))) {return false; }
}
Retour Vrai;
}
La copie de code est la suivante:
// obtient l'objet déclenché par l'événement
fonction getEventTarget (e) {
E = E || window.event;
return e.target || e.srcelement;
}
Le code est simple, mais les fonctions sont très pratiques. Veuillez y consulter si vous en avez besoin.