Here we will sort out the js methods used in my previous project. They are all very common javascript methods. I believe our friends can also use them frequently. Here we will sort them out and share them with you.
Verify whether the image format is used
The Code is as follows:
Function IsImgType (src ){
Var rFilter =/^ (? : Image \/bmp | image \/cis \-cod | image \/gif | image \/ief | image \/jpeg | image \ /pipeg | image \/png | image \/svg \ + xml | image \/tiff | image \/x \-cmu \-raster | image \/x \-cmx | image \/x \-icon | image \/x \-portable \-anymap | image \/x \-portable \-bitmap | image \/x \-portable \-graymap | image \/x \-portable \-pixmap | image \/x \-rgb | image \/x \-xbitmap | image \/x \-xpixmap | image \/x \-xwindowdump) $/I;
Var Filter = /(? : Bmp | cis \-cod | gif | ief | jpeg | pipeg | png | svg \ + xml | tiff | x \-cmu \-raster | x \-cmx | x \-icon | x \-portable \-anymap | x \-portable \-bitmap | x \-portable \-graymap | x \-portable \-pixmap | x \ -rgb | x \-xbitmap | x \-xpixmap | x \-xwindowdump) $/I;
Return rFilter. test (src) | Filter. test (src );
}
Verify if it is color
The Code is as follows:
Function detectColor (value ){
Var pattern =/^ # [0-9a-fA-F] {6} $/; // # XXXXXX
Var result;
Var rgbRegex =/(^ rgb \ (\ d +), \ s * (\ d +), \ s * (\ d +) \) $) | (^ rgba \ (\ d +), \ s * (\ d +), \ s * (\ d +) (, \ s * \ d + \. \ d +) * \) $ )/;
If (pattern. test (value )){
Result = value;
} Else if (rgbRegex. test (value) {// rgba (0, 0, 0, 0)
Result = value;
}
Return result;
}
Convert RGB to HEX:
The Code is as follows:
Function zero_fill_hex (num, digits ){
Var s = num. toString (16 );
While (s. length <digits ){
S = "0" + s;
}
Return s;
}
Function rgb2hex (rgb ){
If (rgb. charAt (0) = '#'){
Return rgb;
}
Var ds = rgb. split (/\ D + /);
Var decimal = Number (ds [1]) * 65536 + Number (ds [2]) * 256 + Number (ds [3]);
Return "#" + zero_fill_hex (decimal, 6 );
}
Verify if it is an Email address:
The Code is as follows:
Function testEmail (value, target ){
Value = value. trim ();
If (! /^ \ W + ([\.-]? \ W +) * @ \ w + ([\.-]? \ W +) * (\. \ w {2, 3}) + $/. test (value )){
Target. val ("");
Alert ("Please fill in the correct E-mail address! ");
}
}
Convert the image src to data 64:
The Code is as follows:
Function createImgData (img ){
Var image = new Image ();
Image. src = img. src | img;
Var tmpCanvas = $ (" ") [0];
Var tmpCtx = tmpCanvas. getContext ("2d ");
If (tmpCanvas ){
TmpCanvas. width = image. width;
TmpCanvas. height = image. height;
TmpCtx. drawImage (image, 0, 0 );
Return tmpCanvas. toDataURL ();
}
}
These are the common js methods used in my recent project. I hope my friends will like them.