This question already has an answer here:
这个问题在这里已有答案:
- How to validate an email address in JavaScript? 74 answers
如何在JavaScript中验证电子邮件地址? 74个答案
I am trying to validate email to access alphanumeric characters
我正在尝试验证电子邮件以访问字母数字字符
<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>
it must contain alphabets and numerics ex:"test123@gmail.com","admin890@gmail.com" , "abc456@gmail.com"
.. how can rewrite this code.??
它必须包含字母和数字ex:“test123@gmail.com”,“admin890 @ gmail.com”,“abc456 @gmail.com”..如何重写此代码。
1 个解决方案
#1
0
I am matching two reg exp as i am not sure how to combine them but it works only for alphanumeric
我匹配两个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>
Here is what happens on 123@gmail.com
这是在123@gmail.com上发生的事情
#1
0
I am matching two reg exp as i am not sure how to combine them but it works only for alphanumeric
我匹配两个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>
Here is what happens on 123@gmail.com
这是在123@gmail.com上发生的事情