Home > Web Front-end > JS Tutorial > body text

Summary of commonly used JavaScript functions and regular verification expression codes

伊谢尔伦
Release: 2017-07-20 10:13:42
Original
1444 people have browsed it

Commonly used regular verification expressions

Mobile phone number verification

var validate = function(num) {
var exp = /^1[3-9]\d{9}$/;
return exp.test(num);
};
Copy after login

ID number verification

var exp = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/;
Copy after login

ip verification

var exp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
Copy after login

Commonly used js functions

$(window).scroll(function() {
var a = $(window).scrollTop();
if(a > 100) {
$('.go-top').fadeIn();
}else {
$('.go-top').fadeOut();
}
});
$(".go-top").click(function(){
$("html,body").animate({scrollTop:"0px"},'600');
});
Copy after login

Prevent bubbling

function stopBubble(e){
e = e || window.event; 
if(e.stopPropagation){
e.stopPropagation(); //W3C阻止冒泡方法 
}else { 
e.cancelBubble = true; //IE阻止冒泡方法 
} 
}
Copy after login

Replace allreplaceAll

var replaceAll = function(bigStr, str1, str2) { //把bigStr中的所有str1替换为str2
var reg = new RegExp(str1, 'gm');
return bigStr.replace(reg, str2);
}
Copy after login

Get the parameter value in the browser url

var getURLParam = function(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
};
Copy after login

Deep copy object

function cloneObj(obj) {
var o = obj.constructor == Object ? new obj.constructor() : new obj.constructor(obj.valueOf());
for(var key in obj){
if(o[key] != obj[key] ){
if(typeof(obj[key]) == 'object' ){
o[key] = mods.cloneObj(obj[key]);
}else{
o[key] = obj[key];
}
}
}
return o;
}
Copy after login

Array deduplication

var unique = function(arr) {
var result = [], json = {};
for (var i = 0, len = arr.length; i < len; i++){
if (!json[arr[i]]) {
json[arr[i]] = 1;
result.push(arr[i]); //返回没被删除的元素
}
}
return result;
};
Copy after login

Determine whether array elements are repeated

var isRepeat = function(arr) { //arr是否有重复元素
var hash = {};
for (var i in arr) {
if (hash[arr[i]]) return true;
hash[arr[i]] = true;
}
return false;
};
Copy after login

Generate random numbers

function randombetween(min, max){
return min + (Math.random() * (max-min +1));
}
Copy after login

Operation cookie

own.setCookie = function(cname, cvalue, exdays){
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = &#39;expires=&#39;+d.toUTCString();
document.cookie = cname + &#39;=&#39; + cvalue + &#39;; &#39; + expires;
};
own.getCookie = function(cname) {
var name = cname + &#39;=&#39;;
var ca = document.cookie.split(&#39;;&#39;);
for(var i=0; i< ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == &#39; &#39;) c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return &#39;&#39;;
};
Copy after login

The above is the detailed content of Summary of commonly used JavaScript functions and regular verification expression codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template