


Summary of commonly used custom public functions in js_basic knowledge
String.prototype.trim = function(){
return this.replace(/(^s*)|(s*$)/g, "");
}
//Check whether it is a date format
function isDate(datestr){
var result = datestr.match(/((^((1[8-9]d{2})|([2 -9]d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$ )|(^((1[8-9]d{2})|([2-9]d{3}))(-)(11|0?[469])(-)(30|[12 ][0-9]|0?[1-9])$)|(^((1[8-9]d{2})|([2-9]d{3}))(-)( 0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0? 2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0 ][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2) (-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9] [0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0 ?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/ );
if(result==null){
return "no";
}
return "yes";
}
//This method has the above effect Consistent
function isDate2(datestr) {
var result = datestr.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2 })$/);
if (result == null)
return "no";
var d = new Date(result[1], result[3] - 1, result[4]) ;
if((d.getFullYear() == result[1] && (d.getMonth() 1) == result[3] && d.getDate() == result[4])){
return "yes";
}
return "no";
}
//Determine whether the entered characters are Chinese
function IsChinese(str){
if (str.length!=0){
reg=/^[u0391-uFFE5] $/;
if(!reg.test(str)){
// alert("Sorry, you entered The string type format is incorrect!");
return "no";
}
}
return "yes";
}
// Determine whether it is empty
function isEmpty(str){
if(str==null||typeof str=="undefined"||str.trim()==""){
return true;
}else{
return false;
}
}
//Landline phone
function testTelephone(phone){
var phone_reg = new RegExp(/^ ([ ]{0,1}d{3,4}|d{3,4}-)?d{7,8}$/);
if(!phone_reg.test(phone)){
return "no";
}
return "yes";
}
//Discount
function isDiscount(discount){
var phone_reg = new RegExp(/^(0 ([.]d{1,2})|1|1.00|1.0)$/);
if(!phone_reg.test(discount)){
return "no";
}
return "yes";
}
//Mobile phone number
function testMobile(mobile){
var mobile_reg = new RegExp(/^0{0,1}1[0-9]{ 10}$/);
if(!mobile_reg.test(mobile)){
return "no";
}
return "yes";
}
//QQ The number starts from 10000
function testQQ(qq){
var qq_reg = new RegExp(/^[1-9][0-9]{4,}$/);
if(!qq_reg. test(qq)){
return "no";
}
return "yes";
}
function testEmail(email){
var email_reg = new RegExp(/^w ([- .]w )*@w ([-.]w )*.w ([-.]w )*$/);
if(!email_reg.test( email)){
return "no";
}
return "yes";
}
//Unsigned positive integer
function testPlusDigit(digit) {
var plusDigit_reg = new RegExp(/^d $/);
if(!plusDigit_reg.test(digit)){
return "no";
}
return "yes" ;
}
//DOUBLE price
function testPriceFormat(str){
var priceFormatReg = new RegExp(/^d (.d{1,2})?$/);
if(!priceFormatReg.test(str)){
return "no";
}
return "yes";
}
//ID card
function testIDCard(str){
var IDCardReg = new RegExp(/(^d{15}$)|(^d{17}([0-9]|X)$)/);
if (!IDCardReg.test(str)){
return "no";
}
return "yes";
}
//2012-06-19 date format
function testDate(str){
var dateReg = new RegExp(/(^d{4}-[0,1][0-9]-[0-3][0-9]$)/) ;
if(!dateReg.test(str)){
return "no";
}
return "yes";
}
//Exact operation of floating point numbers (addition)
function accAdd(arg1,arg2){
var r1,r2,m,n;
try{r1=arg1.toString().split(". ")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0 }
m=Math.pow(10,Math.max(r1,r2));
n=(r1>=r2)?r1:r2;
return ((arg1*m arg2*m )/m).toFixed(n);
}
Number.prototype.add = function (arg){
return accAdd(arg,this);
}
/ /Floating point precise operation (subtraction)
function accSub(arg1,arg2){
return accAdd(arg1,-arg2);
}
Number.prototype.subtract = function (arg){
return accSub(this,arg);
}
//Exact floating point operation (multiplication)
function accMul(arg1,arg2)
{
var m=0 ,s1=arg1.toString(),s2=arg2.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)
}
Number.prototype.mul = function (arg){
return accMul(arg, this);
}
//Exact operation of floating point numbers (division)
function accDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{t1=arg1.toString() .split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}
with(Math){
r1=Number(arg1.toString().replace(".",""))
r2=Number(arg2.toString().replace(".", ""))
return (r1/r2)*pow(10,t2-t1);
}
}
Number.prototype.div = function (arg){
return accDiv (this, arg);
}
//Limit input numbers
function isNumber(e) {
if ($.browser.msie) {
if ( ((event.keyCode > 47) && (event. keyCode < 58)) ||
(event.keyCode == 8) ) {
return true;
} else {
return false;
}
} else {
if ( ((e.which > 47) && (e.which < 58)) ||
(e.which == 8) ) {
return true;
} else {
return false;
}
}
}
//String length interception
function cutstr(str, len) {
var temp;
var icount = 0;
var patrn = /[^x00-xff]/;
var strre = "";
for (var i = 0; i < str.length; i ) {
if (icount < len - 1) {
temp = str.substr(i, 1);
if (patrn.exec(temp) == null) {
icount = icount 1;
} else {
icount = icount 2;
}
strre = temp;
} else {
break
}
}
return strre "...";
}
//Get the domain name host
function getHost(url) {
var host = "null";
if (typeof url == "undefined" || null == url) {
url = window.location.href;
}
var regex = /^w ://([^/]*).*/;
var match = url.match(regex);
if (typeof match != "undefined" && null != match) {
host = match[1];
}
return host ;
}
//Judge whether a value is within the range
//rang=1 means positive integer [0,2147483647] 2 means float[0,3.4028235E38]
/ /return= 'empty' means the input is empty,
function isRang(str,rang){
if(typeof str == "number"){
var num = Number(str);
//Determine whether it is within the positive integer range
if( rang == 1){
if(testPlusDigit(num)=="yes"){
if(num>=0&&num<=2147483647){
return "is_int";
}else{
return "is_not_int_rang";
}
}else{
return "is_not_int";
}
}else if( rang == 2){
if(testPriceFormat(num)=="yes"){
if(num>=0&&num<=3.4028235E38){
return "is_float";
}else {
return "is_not_float_rang";
}
}else{
return "is_not_float";
}
}else{
return "rang_is_not_right";
}
}else{
return "is_not_number";
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file
