Rumah > Artikel > hujung hadapan web > JavaScript验证邮箱的合法性
<script type="text/javascript"> var sText = document.getElementById('mail').value; //onblur 为控件失去焦点事件 document.getElementById('mail').onblur = function sValue(){ sText = this.value; var reMail =/^(?:[a-zA-Z0-9]+[_\-\+\.]?)*[a-zA-Z0-9]+@(?:([a-zA-Z0-9]+[_\-]?)*[a-zA-Z0-9]+\.)+([a-zA-Z]{2,})+$/; var s=new RegExp(reMail); if(s.test(sText)) {alert("邮箱正确");} else//不正确清空 {document.getElementById('mail').value="";} } </script>