function chkfrm(o){
if(!/\d+/gi.test(o.empid.value)){
alert('雇员编号:只能是数字。')
return false
}
if(o.empname.value==''){
alert('雇员姓名:不能为空。')
return false
}
if(o.empjob.value==''){
alert('雇员工作:不能为空。')
return false
}
if(!/^((((1[6-9]|[2-9]\d)\d{2})[-\/](0?[13578]|1[02])[-\/](0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})[-\/](0?[13456789]|1[012])[-\/](0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})[-\/]0?2[-\/](0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))[-\/]0?2[-\/]29[-\/]))$/gi.test(o.emptime.value)){
alert('雇佣日期:必须是日期格式,即2013-03-02')
return false
}
if(isNaN(o.empmoney.value)){
alert('基本工资:必须是数字(小数)')
return false
}
if(isNaN(o.emprw.value)){
alert('奖金:必须是数字(小数)')
return false
}
}
将
<form action="" method="post">
改为
<form action="" method="post" onsubmit="javascript:return chkfrm(this)">
即可
用正则表达式,if判断内容是否为空,为空则提示输入手机号,不为空再用正则验证内容是否符合格式
附上源码
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style type="text/css">
*{margin: 0padding: 0}
#box{
width: 240px
height: 100px
border:1px solid gray
border-radius: 10px
padding: 30px 10px 10px 10px
margin: 100px auto 0
}
#tel{
width: 160px
height: 20px
margin: 0
padding-left:5px
}
#btn{
width: 50px
height: 25px
}
p{
margin-top: 10px
font-size: 14px
font-family: 'Microsoft YaHei'
}
</style>
</head>
<body>
<div id="box">
<input type="" name="" id="tel">
<input type="button" name="" value="确认" id="btn">
<p></p>
</div>
<script type="text/javascript">
var oBox=document.getElementById('box')
var oBtn=document.getElementById('btn')
var oP=document.getElementsByTagName('p')[0]
oBtn.onclick=function(){
var val=document.getElementById('tel').value
var reg=/^1[345678]\d{9}$/
if (val=='') {
oP.style.color="red"
oP.innerHTML="请输入手机号!"
}else if(reg.test(val)){
oP.style.color="green"
oP.innerHTML="输入正确!"
}else{
oP.style.color="red"
oP.innerHTML="输入错误,请重新输入!"
}
}
</script>
</body>
</html>