如何验证字母数字中的电子邮件ID

时间:2016-05-05 10:57:25

标签: javascript ajax

我正在尝试验证电子邮件以访问字母数字字符

<script type = "text/javascript">
function checkField(email)
{
if (/[^0-9a-bA-B\s]/gi.test(email.value))
{
alert ("Only alphanumeric characters and spaces are valid in 
this field");
email.value = "";
email.focus();
return false;
}
}
</script>

它必须包含字母和数字ex:"test123@gmail.com","admin890@gmail.com" , "abc456@gmail.com" ..如何重写此代码。 enter image description here

1 个答案:

答案 0 :(得分:0)

我匹配两个reg exp,因为我不知道如何组合它们但它只适用于字母数字

<form method="post" name="client_form" id="client_form"> <input name="email" id="email" type="text" value="" class="formq" tabindex="1" style="width:200px" autocomplete="off" onblur="checkField(this.value)"/>
</form>
<script>
function checkField(email){ 
console.log(email);
      var reg_exp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
      var chk = reg_exp.test(email);
      var checkalphanumeric = email.split("@");
      var alphanumeric_chk = /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i;
      var check2 = alphanumeric_chk.test(checkalphanumeric[0]);
      console.log(check2);
      if(chk && check2){
          alert("valid");
      } else { 
          alert("Only alphanumeric characters and spaces are valid in this field"); 
      }
}
</script>

这是在123@gmail.com上发生的事情 enter image description here