Common JS methods in projects _ javascript tips-js tutorials

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.