1 手机号验证
<input type="tel" id="phone" name="phone" placeholder="请输入联系电话" maxlength="11" onkeyup="this.value=this.value.replace(/[^0-9]/g,‘‘)" onafterpaste="this.value=this.value.replace(/[^0-9]/g,‘‘)" > <script> function checkPhone(phone){ var regu = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$)/; if(!(regu.test(phone))){ return true; } else{ return false; } } $("#phone").blur(function(){ //手机验证 var tel=$(this)[0]; if(tel.value.length!=11){ console.log("请输入正确的手机号");//增加错误提示 $(‘[name="phone"]‘).focus(); return ; } if(tel.value.length==11){ if(checkPhone(tel.value)){ console.log("请填写有效的11位手机号码");//错误提示 $(‘[name="phone"]‘).focus(); return ; } } }); </script>
2 email验证
<input type="email" id="email" name="email" placeholder="请输入电邮地址" onblur="testEmail($(this).val())"> <script> function testEmail(str){ var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9][email protected]([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/; if(!reg.test(str)){ console.log("请输入正确的电邮地址");//错误提示 $(‘[name="email"]‘).focus(); return ; } } </script>
时间: 2024-10-10 02:59:53