Home >Web Front-end >JS Tutorial >Some JS code for checking data_javascript tips

Some JS code for checking data_javascript tips

WBOY
WBOYOriginal
2016-05-16 19:26:301088browse

These JS codes are often used when submitting forms, and most of the codes are also written with regular expressions!


//Remove the left space;
function ltrim(s){
return s.replace( /^s*/, "");
}
//Remove the right space;
function rtrim(s){
return s.replace( /s*$/, "");
}
//Remove left and right spaces;
function trim(s){
return rtrim(ltrim(s));
}
//Whether it is an empty value;
function IsEmpty(_str){
var tmp_str = trim(_str);
return tmp_str.length == 0;
}
//Whether it is a valid email;
function IsMail(_str){
var tmp_str = trim(_str);
var pattern = /^[_a-z0-9-] (.[_a-z0- 9-] )*@[a-z0-9-] (.[a-z0-9-] )*$/;
return pattern.test(tmp_str);
}
//Yes Valid number;
function IsNumber(_str){
var tmp_str = trim(_str);
var pattern = /^[0-9]/;
return pattern.test(tmp_str);
}
//Whether the color value is valid;
function IsColor(color){
var temp=color;
if (temp=="") return true;
if (temp.length!=7) return false;
return (temp.search(/#[a-fA-F0-9]{6}/) != -1);
}
/ / Whether the link is valid;
function IsURL(url){
var sTemp;
var b=true;
sTemp=url.substring(0,7);
sTemp=sTemp. toUpperCase();
if ((sTemp!="HTTP://")||(url.length<10)){
b=false;
}
return b;
}
//Whether the mobile phone number is valid;
function IsMobile(_str){
var tmp_str = trim(_str);
var pattern = /13d{9}/;
return pattern.test(tmp_str);
}

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