使用正则表达式进行表单验证
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html> 3 <head> 4 <title>表单验证_手机号</title> 5 <script> 6 7 function checkForm(o){ 8 var re=/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/; 9 if(!re.test(o.tel.value)){ 10 alert('请输入正确的手机号码。'); 11 return false; 12 } 13 } 14 </script> 15 </head> 16 <body> 17 <form class="form" method="post" id="form" onsubmit="return checkForm(this);" action=""> 18 19 电话号码:<input type="text" maxlength="11" name="tel" placeholder="手机号码"/> 20 21 <input type="submit" name="submit" value="提交"/> 22 23 </form> 24 </body> 25 </html>
以上运行结果示例:
成功不是终点,失败也并非末日,重要的是前行的勇气!