我正在验证电子邮件地址。但这并不能验证我是否使用了正确的 tld。我只想验证 .com、.in、.org、.gov 和 .jo 。如何做到这一点?我的代码是这样的:
function validateConsumerEmail(){
email = document.getElementById('customer_email').value;
if ((email == null)||(email == "")){
alert("Please Enter a Valid Consumer Email Address...")
document.consumerEmail.customer_email.focus();
return false
}
if (echeck(email)==false){
email=""
document.consumerEmail.customer_email.focus();
return false
}
if(email != ''){
var splitting = email.split('@');
if(!isNaN(splitting[0])){
alert('Please provide proper email address...');
document.consumerEmail.customer_email.focus();
return false;
}
if(splitting[0].length<6 || splitting[0].length > 250){
alert('Please provide proper email address...');
document.consumerEmail.customer_email.focus();
return false;
}
}
}
其中 customer_email 是字段名称的 id。
function echeck(str) {
var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert("Please provide valid email ID.");
return false
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert("Please provide valid email ID.");
return false
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert("Please provide valid email ID.");
return false
}
if (str.indexOf(at,(lat+1))!=-1){
alert("Please provide valid email ID.");
return false
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert("Please provide valid email ID.");
return false
}
if (str.indexOf(dot,(lat+2))==-1){
alert("Please provide valid email ID.");
return false
}
if (str.indexOf(" ")!=-1){
alert("Please provide valid email ID.");
return false
}
return true
}