function isEmpty(_obj,flag){ var obj = document.getElementById(_obj.id); var info = document.getElementById(_obj.id+"Info"); if(flag){ if(obj.value.length == 0){ showInfo(info,"該內容不可為空","red") return false;} else{ showInfo(info,"√","green") return true;} } else{ showInfo(info,"請輸入相應表單內容!","blue") return false; } } //驗證郵編,內容非必要欄位,如果填寫則進行驗證 function isPostCode(_obj,flag){ var obj = document.getElementById(_obj.id); var info = document.getElementById(_obj.id+"Info"); var reg = /^d{6}$/; if(flag){ if(obj.value.length>0){ if(reg.test(obj.value)==false){ showInfo(info,"請輸入6位元字為合法的郵遞區號格式!","red") return false;} else{ showInfo(info,"√","green") return true;} } else{ showInfo(info,"郵編為可選填的內容","black") return true;} } else{ showInfo(info,"郵編的格式為6位元字","blue") } } //驗證電子郵件 //參數:Email表單元素ID,是否有必填,表單狀態 function isEmail(_obj,isempty,flag){ var obj = document.getElementById(_obj.id); var info = document.getElementById(_obj.id+"Info"); var reg =/^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/; if (flag){ if(isempty){ if(obj.value == ""){ showInfo(info,"電子郵件不可為空","red") return false; } if (reg.test(obj.value)==false){ showInfo(info,"電子郵件格式不正確","red") return false;} else{ showInfo(info,"√","green") return true;} } else{ if (obj.value.length>0){ if (reg.test(obj.value)==false){ showInfo(info,"電子郵件格式不正確","red") return false;} else{ showInfo(info,"√","green") return true; } } else{ showInfo(info,"如果沒有,也可以不留!","black") return true; } } } else{ showInfo(info,"填寫此項以便我們與您取得聯絡","blue") } } //電話驗證:非必填內容 function isPhone(_obj,flag){ var obj=document.getElementById(_obj.id); var info=document.getElementById(_obj.id+"Info"); var reg=/(^[0-9]{3,4}-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/; if(flag){ if(obj.value.length>0){ if(reg.test(obj.value)==false){ showInfo(info,"電話格式不正確","red") return false; } else{ showInfo(info,"√","green") return true; } } else{ showInfo(info,"如果方便請填寫此項!","black") return true; } } else{ showInfo(info,"如果方便請填寫此項!","blue") } } //顯示資訊 function showInfo(_info,msg,color){ var info=_info; info.innerHTML = msg; info.style.color=color; } <script language="javascript" type="text/javascript"> function checkform(frm){ var refalg=false; var f1,f2,f3,f4,f5,f6,f7; f1 = isEmpty(frm.title,1) f2 = isEmpty(frm.name,1) f3 = isEmpty(frm.addr,1) f4 = isEmpty(frm.content,1) f5 = isPhone(frm.tel,1) f6 = isEmail(frm.mail,0,1) f7 = isEmail(frm.msn,0,1) refalg = f1 && f2 && f3 && f4 && f5 && f6 && f7 // alert(refalg) // return refalg; if(refalg) { frm.submit(); } else { alert("請檢查表單內容是否填寫完整!"); } } </script> |