javascript 使用没有正则表达式的 jscript 验证电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20588026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validating email address using jscript without a regular expression
提问by cpp-coder
We can perform a very basic validation of email address with JavaScript by implementing the following three rules:
我们可以通过实现以下三个规则来使用 JavaScript 对电子邮件地址进行非常基本的验证:
1.The email address must have @ character
1.邮箱地址必须有@字符
2.The email address must have .(dot) character
2.电子邮件地址必须有.(点)字符
3.There must be atleast 2 characters between @ and .(dot)
3.@和.(点)之间必须至少有2个字符
回答by Jason
This satisfies all the rules you stated as well as not allowing @ to start the address and not allowing . to end the address. It does not account for multiple . in the address.
这满足您陈述的所有规则以及不允许 @ 开始地址并且不允许 . 结束地址。它不占多个 . 在地址中。
function testEmailAddress(emailToTest) {
// check for @
var atSymbol = emailToTest.indexOf("@");
if(atSymbol < 1) return false;
var dot = emailToTest.indexOf(".");
if(dot <= atSymbol + 2) return false;
// check that the dot is not at the end
if (dot === emailToTest.length - 1) return false;
return true;
}
回答by Arun Raj
``This is working...My own Creation :)Email Validation
``这是有效的......我自己的创作:)电子邮件验证
<input type=”text” name=”email” id=”email” />
<input type=”button” name=”btnok” onclick=”validate()” />
Java Script Function
Java 脚本函数
<script type=”text/javascript”>
function validate()
{
var str;
var t=1;
str =document.getElementById(‘email').value;
if(document.getElementById(‘email').value==”")
{
alert(“Empty”);
}
var res = str.split(‘@');
if(str.split(‘@').length!=2)
{
alert(“zero @ OR morethan one @ “);
t=0;
}
var part1=res[0];
var part2=res[1];
// part1
if(part1.length==0)
{
alert(“no content bfr @”);
t=0;
}
if(part1.split(” “).length>2)
{
alert(“Invalid:Space before @”)
t=0;
}
//chk afr @ content: part2
var dotsplt=part2.split(‘.'); //alert(“After @ :”+part2);
if(part2.split(“.”).length<2)
{
alert(“dot missing”);
t=0;
}
if(dotsplt[0].length==0 )
{
alert(“no content b/w @ and dot”);
t=0;
}
if(dotsplt[1].length<2 ||dotsplt[1].length>4)
{alert(“err aftr dot”);
t=0;
}
if(t==1)
alert(“woooooooooooooooooooowwwww…it is a valid email”);
}
</script>
回答by Krish R
Can you try this,
你可以试试这个吗
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
回答by cpp-coder
var checkEmail = function(value) {
var valid = true;
if (value.indexOf('@') == -1) {
valid = false;
} else {
var parts = value.split('@');
var domain = parts[1];
if (domain.indexOf('.') == -1) {
valid = false;
} else {
var domainParts = domain.split('.');
var ext = domainParts[1];
if (ext.length > 4 || ext.length < 2) {
valid = false;
}
}
}
return valid;
};
var form = document.getElementById('test');
var validate = function(event) {
event.preventDefault();
var val = document.getElementById('email').value;
var valid = checkEmail(val);
if (!valid) {
alert('Not a valid e-mail address');
} else {
alert('Valid e-mail address');
}
};
form.addEventListener('submit', validate, false);
There are many techniques of validating email address, each validation method has its own pros and cons. The above method doesn't require understanding of regular expressions
验证电子邮件地址的技术有很多种,每种验证方法都有其优缺点。上述方法不需要理解正则表达式