<form onsubmit="return check();">
<input type="text" name="t1">
<input type="submit">
</form>
<script language="javascript">
--------------------------------檢查數字(整數)
function check() {
re = /^\d+$/;
if (!re.test(document.forms[0].t1.value)) {
alert("欄位不能空白且只允許輸入數字");
document.forms[0].t1.focus();
return false;
}
return true;
}
--------------------------------檢查數字可有小數
function check(thisname) {
if (isNaN(document.getElementById(thisname).value)) {
alert("欄位不能空白且只允許輸入數字");
document.getElementById(thisname).focus();
return false;
}
}
--------------------------------檢查是否有填資料
function gbchk(theForm)
{
if (theForm.name.value == "")
{
alert("請輸入姓名!!");
theForm.name.focus();
return (false);
}
if (theForm.content.value == "")
{
alert("請輸入留言內容!!");
theForm.content.focus();
return (false);
}
return (true);
}
//-->
----------------------------------檢查是否為MAIL格式
re1 = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;if (re1.exec(theForm.email.value) == null)
{
alert("你的電子郵件格式不合!");
theForm.email.focus();
return (false);
}
</script>
----------------------------------檢查日期格式
<script type="text/javascript" language="JavaScript">
function dateValidationCheck(str){
var re = new RegExp("^([0-9]{4})[./]{1}([0-9]{1,2})[./]{1}([0-9]{1,2})$");
var strDataValue;
var infoValidation = true;
if ((strDataValue = re.exec(str)) != null){
var i;
i = parseFloat(strDataValue[1]);
if (i <= 0 || i > 9999){ // 年
infoValidation = false;
}
i = parseFloat(strDataValue[2]);
if (i <= 0 || i > 12){ // 月
infoValidation = false;
}
i = parseFloat(strDataValue[3]);
if (i <= 0 || i > 31){ // 日
infoValidation = false;
}
}else{
infoValidation = false;
}
if (!infoValidation){
alert('請輸入 YYYY/MM/DD 日期格式');
}
return infoValidation;
}
</script>
留言列表