Home  >  Article  >  Web Front-end  >  Organizing JS methods commonly used in projects_javascript skills

Organizing JS methods commonly used in projects_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:16:551014browse

Verify whether it is an image format

Copy code The code is as follows:

function IsImgType(src) {
var rFilter = /^(?:image/bmp|image/cis-cod|image/gif|image/ief|image/jpeg|image/jpeg|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|jpeg|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

Copy code 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:

Copy code 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 whether it is an email address:

Copy code 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 image src to data 64:

Copy code 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();
}
}

The above are the commonly used js methods that I have used in recent projects. I have sorted them out. I hope your friends will like them.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn