function validateEmail(){
var TCode = document.getElementById('email').value;
if(TCode.length==0)
{
email_info.innerHTML="This field is required";
return false;
}
email_info.innerHTML=" ";
var atpos=TCode.indexOf("@");
var dotpos=TCode.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=TCode.length)
{
email_info.innerHTML="Not a valid e-mail address";
return false;
}
email_info.innerHTML=" ";
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("email_info").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_email.jsp?email="+TCode,true);
xmlhttp.send();
return true;
}
这是我的函数,在文本字段上调用onblur操作,该文本字段带有用户电子邮件ID ...我想要做的是限制表单提交,如果电子邮件ID已经存在于数据库中...我是能够显示已存在电子邮件ID的文本但我无法限制用户提交表单,即使它已经存在该怎么办?
答案 0 :(得分:0)
首先将变量设置为false
window.isOkayToSubmit = false;
然后在检查完电子邮件后,将该变量设置为true:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("email_info").innerHTML=xmlhttp.responseText;
window.isOkayToSubmit = true;
}
然后在表单提交:
<form onsubmit="return isValidForm()" />
你只需要返回那个val:
function isValidForm(){
return window.isOkayToSubmit;
}
另外,如果我理解你的代码,你会在电子邮件出错时将其设置为false,以防somenoe提供好的电子邮件,然后将其更改为错误:
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=TCode.length)
{
email_info.innerHTML="Not a valid e-mail address";
window.isOkayToSubmit = false;
return false;
}