各種條件判斷代碼
//去掉空格
function Trim(str){
if(str.charAt(0) == " "){
? str = str.slice(1);
? str = Trim(str);
}
return str;
}
//判斷是否是空
function isEmpty(pObj,errMsg){
var obj = eval(pObj);
if( obj == null || Trim(obj.value) == ""){
? if (errMsg == null || errMsg =="")
?? alert("輸入為空!");
? else
?? alert(errMsg);
? obj.focus();
? return false;
}
return true;
}
//判斷是否是數(shù)字
function isNumber(pObj,errMsg){
var obj = eval(pObj);
strRef = "1234567890";
if(!isEmpty(pObj,errMsg))return false;
for (i=0;i<obj.value.length;i++) {
? tempChar= obj.value.substring(i,i+1);
? if (strRef.indexOf(tempChar,0)==-1) {
?? if (errMsg == null || errMsg =="")
??? alert("數(shù)據(jù)不符合要求,請(qǐng)檢查");
?? else
??? alert(errMsg);
?? if(obj.type=="text")
??? obj.focus();
?? return false;
? }
}
return true;
}
//判斷是否是數(shù)字,數(shù)字可以為負(fù)數(shù)
function isNegative(pObj,errMsg){
var obj = eval(pObj);
strRef = "1234567890-";
if(!isEmpty(pObj,errMsg))return false;
for (i=0;i<obj.value.length;i++) {
? tempChar= obj.value.substring(i,i+1);
? if (strRef.indexOf(tempChar,0)==-1) {
?? if (errMsg == null || errMsg =="")
??? alert("數(shù)據(jù)不符合要求,請(qǐng)檢查");
?? else
??? alert(errMsg);
?? if(obj.type=="text")
??? obj.focus();
?? return false;
? }else{
?? if(i>0){
??? if(obj.value.substring(i,i+1)=="-"){
???? if (errMsg == null || errMsg =="")
????? alert("數(shù)據(jù)不符合要求,請(qǐng)檢查");
???? else
????? alert(errMsg);??
???? if(obj.type=="text")
???? obj.focus();
???? return false;
??? }
?? }
? }
}
return true;
}
//判斷是否是錢的形式
function isMoney(pObj,errMsg){
? var obj = eval(pObj);
? strRef = "1234567890.";
? if(!isEmpty(pObj,errMsg)) return false;
??? for (i=0;i<obj.value.length;i++) {
???? tempChar= obj.value.substring(i,i+1);
???? if (strRef.indexOf(tempChar,0)==-1) {
????? if (errMsg == null || errMsg =="")
??? alert("數(shù)據(jù)不符合要求,請(qǐng)檢查");
????? else
??? alert(errMsg);??
??? if(obj.type=="text")
??? obj.focus();
??? return false;
??? }
??? else{
??? tempLen=obj.value.indexOf(".");
??? if(tempLen!=-1){
????? strLen=obj.value.substring(tempLen+1,obj.value.length);
????? if(strLen.length>2){
??????? if (errMsg == null || errMsg =="")
??????? alert("數(shù)據(jù)不符合要求,請(qǐng)檢查");
??????? else
??????? alert(errMsg);??
??????? if(obj.type=="text")
????????? obj.focus();
????????? return false;
?????? }
????? }
???? }
? }
? return true;
}
function isLeapYear(year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
return true;
}?
return false;
}
//判斷時(shí)間是否正確
function isDate(checktext){
var datetime;
var year,month,day;
var gone,gtwo;
if(Trim(checktext.value)!=""){
datetime=Trim(checktext.value);
if(datetime.length==10){
? year=datetime.substring(0,4);
? if(isNaN(year)==true){
?? alert("請(qǐng)輸入日期!格式為(yyyy-mm-dd) \n例(2001-01-01)!");
?? checktext.focus();
?? return false;
? }
? gone=datetime.substring(4,5);
? month=datetime.substring(5,7);
? if(isNaN(month)==true){
?? alert("請(qǐng)輸入日期!格式為(yyyy-mm-dd) \n例(2001-01-01)!");
?? checktext.focus();
?? return false;
? }
? gtwo=datetime.substring(7,8);
? day=datetime.substring(8,10);
? if(isNaN(day)==true){
?? alert("請(qǐng)輸入日期!格式為(yyyy-mm-dd) \n例(2001-01-01)!");
?? checktext.focus();
?? return false;
? }
? if((gone=="-")&&(gtwo=="-")){
?? if(month<1||month>12) {
??? alert("月份必須在01和12之間!");
??? checktext.focus();
??? return false;
??? }
?? if(day<1||day>31){
??? alert("日期必須在01和31之間!");
??? checktext.focus();
??? return false;
?? }else{
??? if(month==2){?
???? if(isLeapYear(year)&&day>29){
?????? alert("二月份日期必須在01到29之間!");
?????? checktext.focus();
?????? return false;
???? }??????
???? if(!isLeapYear(year)&&day>28){
?????? alert("二月份日期必須在01到28之間!");
?????? checktext.focus();
?????? return false;
???? }
??? }
??? if((month==4||month==6||month==9||month==11)&&(day>30)){
???? alert("在四,六,九,十一月份 \n日期必須在01到30之間!");
???? checktext.focus();
???? return false;
??? }
?? }
? }else{
?? alert("請(qǐng)輸入日期!格式為(yyyy-mm-dd) \n例(2001-01-01)");
?? checktext.focus();
?? return false;
? }
}else{
? alert("請(qǐng)輸入日期!格式為(yyyy-mm-dd) \n例(2001-01-01)");
? checktext.focus();
? return false;
}
}else{
return true;
}
return true;
}
//判斷是否月份正確
function isYearMonth(checktext){
? var datetime;
? var year,month,day;
? var gone,gtwo;
? if(Trim(checktext.value)!=""){
??? datetime=Trim(checktext.value);
??? if(datetime.length==7){
??? year=datetime.substring(0,4);
??? if(isNaN(year)==true){
???? alert("輸入錯(cuò)誤!格式為(yyyy-mm) \n例(2001-01)!");
???? checktext.focus();
???? return false;
??? }
??? gone=datetime.substring(4,5);
??? month=datetime.substring(5,7);
??? if(isNaN(month)==true){
???? alert("輸入錯(cuò)誤!格式為(yyyy-mm) \n例(2001-01)!");
???? checktext.focus();
???? return false;
??? }
??? if((gone=="-")){
????? if(month<1||month>12) {
?????? alert("月份必須在01和12之間!");
?????? checktext.focus();
?????? return false;
?????? }
??? }
??? else{
????? alert("輸入錯(cuò)誤!格式為(yyyy-mm) \n例(2001-01)!");
????? checktext.focus();
????? return false;??
??? }
?? }
??? else{
????? alert("輸入錯(cuò)誤!格式為(yyyy-mm) \n例(2001-01)!");
????? checktext.focus();
????? return false;
??? }
}
? return true;
}
function fucCheckNUM(NUM) {
? if (NUM.length!=11)
?? return false;
????
? var i,j,strTemp;
? strTemp="0123456789";
? if ( NUM.length== 0)
?? return false;
? if ( NUM.length== undefined||NUM==undefined)
?? return false;
? for (i=0;i<NUM.length;i++)
? {
?? j=strTemp.indexOf(NUM.charAt(i));
?? if (j==-1)
?? {
?? //說(shuō)明有字符不是數(shù)字
?? return false;
?? }
? }
? //說(shuō)明是數(shù)字
? return true;
}
?
function click () {
? //alert ('不許偷看!禁止使用鼠標(biāo)右鍵!');
? window.event.returnValue=false;
}