最常用的JS匯總,精心整理,供大家參考!!!請高手繼續增加,大家一起頂起!!
//檢查空串
function isEmpty(str){
if((str == null)||(str.length == 0)) return (true);
else return(false);
}
//檢查是否未數字
function isDigit(theNum){
var theMask = "0123456789";
if (isEmpty(theNum)) return(false);
else if(theMask.indexOf(theNum) == -1) return(false);
return(true);
}
//去掉左空格
function trimLeft(str){
if(str.charAt(0) == " "){
str = str.slice(1);
str = trimLeft(str);
}
return str;
}
//去掉右空格
function trimRight(str){
if(str.charAt( str.length - 1 ) == " "){
str = str.slice(0,str.length - 1);
str = trimRight(str);
}
return str;
}
//去掉左右空格
function trim(str){
return trimLeft(trimRight(str));
}
//檢查是否是int
function isInt(str){
if(str==""){
return (false);
}
else{
for(i=0;i<str.length;i++){
var chr = str.charAt(i);
if(!(chr>='0' && chr<='9')){
return (false);
}
}
}
return (true);
}
//檢查str是小數,它的整數部分不多于i位,小數部分不多于j位
function isDecimalNoMsg(str,i,j){
var dot = str.indexOf(".");
var dot_last = str.lastIndexOf(".");
var str_f = "";
var str_b = "";
if ( dot != -1 ){
str_f = str.substring(0,dot);
} else {
str_f = str;
}
if ( dot_last != -1 ){
str_b = str.substring(dot+1);
} else {
str_b = str;
}
if( isInt( str_f ) == false ){
//alert(strMsg);
return false;
} else if ( isInt( str_b ) == false ){
//alert(strMsg);
return false;
} else if ( dot != dot_last ){
//alert(strMsg);
return false;
} else if(dot==0 || dot_last==0){
//alert(strMsg);
return false;
}
if(str_f.length>i){
//alert(strMsg);
return false;
}
if(dot!=-1 && str_b.length>j){
//alert(strMsg);
return false;
}
return true;
}
//檢查日期格式Format: 99999999)
function judgeDateFormat(dateStr){
var re,r;
re = /\d{8}/;
r = dateStr.match(re);
return(r);
}
//檢查時間格式(Format: 999999)
function judgeTimeFormat(timeStr){
var re,r;
re = /\d{6}/;
r = timeStr.match(re);
return(r);
}
//檢查日期是否合法(Format: YYYYMMDD).
function validateDate(theStr){
if(theStr.length!=8){
return (false);
} else {
if(theStr=="99999999")
return (true);
var y = theStr.substring(0,4);
var m = theStr.substring(4,6);
var d = theStr.substring(6,8);
var maxDays = 31;
if(isInt(m)==false||isInt(d)==false||isInt(y)==false){
return(false);
}
else if (y.length < 4){ return(false);}
else if (!isBetween(m,1,12)){ return(false);}
if(m.length!=2){ return(false);}
else if (m==4||m==6||m==9||m==11)maxDays = 30;
else if (m==2){
if(y%4>0)maxDays = 28;
else if(y%100==0&&y%400>0)maxDays = 28;
else maxDays = 29;
}
if(isBetween(d,1,maxDays)==false){ return(false);}
if(d.length!=2){ return(false);}
return(true);
}
}
//檢查時間是否合法(Format: HHMMSS).
function validateTime(theStr){
if(theStr.length!=6){
return (false);
} else {
if(theStr == "240000"){
return (true);
}
var h = theStr.substring(0,2);
var m = theStr.substring(2,4);
var s = theStr.substring(4,6);
if(isInt(h)==false||isInt(m)==false||isInt(s)==false){
return(false);
}
else if (h.length < 2){ return(false);}
else if (!isBetween(h,0,23)){ return(false);}
else if (!isBetween(m,0,59)){ return(false);}
else if (!isBetween(s,0,59)){ return(false);}
return true;
}
}
//檢查str包含漢字時最大長度不大于maxLen
function ChineseLenLimit( str, maxLen){
var Strs = str;
var strlength=0;
var i;
for ( i=0;i<str.length;i++) {
if(str.charCodeAt(i)>=1000)
strlength += 2;
else
strlength += 1;
}
if ( strlength > maxLen ){
return false;
}
return true;
}
//檢查val在lo與hi之間
function isBetween(val,lo,hi){
if ((val < lo) || (val > hi)){ return(false);}
else { return(true);}
}
// 檢查charC是‘0’~‘9’
function chkChar(charC) {
if (charC == null || charC.length == 0) {
return false;
}
if (charC == '0') return true;
if (charC == '1') return true;
if (charC == '2') return true;
if (charC == '3') return true;
if (charC == '4') return true;
if (charC == '5') return true;
if (charC == '6') return true;
if (charC == '7') return true;
if (charC == '8') return true;
if (charC == '9') return true;
return false;
}
// 檢查intI是正整數
function chkInt(intI) {
if (intI == null || intI.length == 0) {
return false;
}
for(var i=0;i<intI.length;i++) {
if (!chkChar(intI.charAt(i))) {
return false;
}
}
if (intI.charAt(0) == '0') {
return false;
}
return true;
}
// 檢查numN是數字
function chkNumber(numN) {
if (numN == null || numN.length == 0) {
return false;
}
for(var i=0;i<numN.length;i++) {
if (!chkChar(numN.charAt(i))) {
return false;
}
}
if (numN.length > 1 && numN.charAt(0) == '0') {
return false;
}
return true;
}
// 檢查錢數小數點后最多2位
function chkAmount(amtA) {
if (amtA == null || amtA.length == 0) {
return false;
}
var amtArray = new Array();
amtArray = amtA.split(".");
if (amtArray.length > 2) {
return false;
}
if (amtArray.length == 1) {
if (!chkNumber(amtArray[0])) {
return false;
}
return true;
}
if (!chkNumber(amtArray[0])) {
return false;
}
if (amtArray[1].length > 2) {
return false;
}
for(var i=0;i<amtArray[1].length;i++) {
if (!chkChar(amtArray[1].charAt(i))) {
return false;
}
}
return true;
}