关于js表单验证的问题 30
在网上找了一段无刷新后台验证客户电话号码1和电话号码2是否已存在的代码,具体如下://检测电话号码1是否有重functioncheckTN1(){TelNo1=docum...
在网上找了一段无刷新后台验证客户电话号码1和电话号码2是否已存在的代码,具体如下:
//检测电话号码1是否有重
function checkTN1()
{
TelNo1=document.getElementById("TelNo1").value;
var xmlhttp;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
}
}
}
if (TelNo1=="")
{
msg="必填项";
var ch=document.getElementById("check1");
ch.innerHTML="<font color='red'>"+msg+"</font>";
return false;
}
//创建请求
xmlhttp.open("get","Check_TELNo1.asp?TelNo1="+escape(TelNo1)+ "&t=" + new Date().getTime());
xmlhttp.onreadystatechange=function()
{
if(4==xmlhttp.readyState)
{
if(200==xmlhttp.status)
{
if (xmlhttp.responseText=="yes")
{
msg="<font color='red'>×</font>";
}
else
{
msg="√";
}
}
else
{
msg="网络链接失败";
}
var ch=document.getElementById("check1");
ch.innerHTML="<font color='#0000ff'>"+msg+"</font>";
}
}
xmlhttp.send(null);
return false;
}
// 检测电话号码2是否有重
function checkTN2()
{
TelNo2=document.getElementById("TelNo2").value;
var xmlhttp;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
}
}
}
if (TelNo2=="")
{
msg="若有请输入";
var ch=document.getElementById("check2");
ch.innerHTML="<font color='0000ff'>"+msg+"</font>";
return false;
}
//创建请求,并使用escape对username编码,以避免乱码
xmlhttp.open("get","Check_TELNo2.asp?TelNo2="+escape(TelNo2)+ "&t=" + new Date().getTime());
xmlhttp.onreadystatechange=function()
{
if(4==xmlhttp.readyState)
{
if(200==xmlhttp.status)
{
if (xmlhttp.responseText=="yes")
{
msg="<font color='red'>×</font>";
}
else
{
msg="√";
}
}
else
{
msg="网络链接失败";
}
var ch=document.getElementById("check2");
ch.innerHTML="<font color='#0000ff'>"+msg+"</font>";
}
}
xmlhttp.send(null);
return false;
}
现在想得到的效果是,当电话号码1或电话号码2只要有一项已经存在了,应该也就是:if (xmlhttp.responseText=="yes")时,这个表单里的【提交】按钮是不可用的。
请各位高手教我,谢谢! 展开
//检测电话号码1是否有重
function checkTN1()
{
TelNo1=document.getElementById("TelNo1").value;
var xmlhttp;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
}
}
}
if (TelNo1=="")
{
msg="必填项";
var ch=document.getElementById("check1");
ch.innerHTML="<font color='red'>"+msg+"</font>";
return false;
}
//创建请求
xmlhttp.open("get","Check_TELNo1.asp?TelNo1="+escape(TelNo1)+ "&t=" + new Date().getTime());
xmlhttp.onreadystatechange=function()
{
if(4==xmlhttp.readyState)
{
if(200==xmlhttp.status)
{
if (xmlhttp.responseText=="yes")
{
msg="<font color='red'>×</font>";
}
else
{
msg="√";
}
}
else
{
msg="网络链接失败";
}
var ch=document.getElementById("check1");
ch.innerHTML="<font color='#0000ff'>"+msg+"</font>";
}
}
xmlhttp.send(null);
return false;
}
// 检测电话号码2是否有重
function checkTN2()
{
TelNo2=document.getElementById("TelNo2").value;
var xmlhttp;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
}
}
}
if (TelNo2=="")
{
msg="若有请输入";
var ch=document.getElementById("check2");
ch.innerHTML="<font color='0000ff'>"+msg+"</font>";
return false;
}
//创建请求,并使用escape对username编码,以避免乱码
xmlhttp.open("get","Check_TELNo2.asp?TelNo2="+escape(TelNo2)+ "&t=" + new Date().getTime());
xmlhttp.onreadystatechange=function()
{
if(4==xmlhttp.readyState)
{
if(200==xmlhttp.status)
{
if (xmlhttp.responseText=="yes")
{
msg="<font color='red'>×</font>";
}
else
{
msg="√";
}
}
else
{
msg="网络链接失败";
}
var ch=document.getElementById("check2");
ch.innerHTML="<font color='#0000ff'>"+msg+"</font>";
}
}
xmlhttp.send(null);
return false;
}
现在想得到的效果是,当电话号码1或电话号码2只要有一项已经存在了,应该也就是:if (xmlhttp.responseText=="yes")时,这个表单里的【提交】按钮是不可用的。
请各位高手教我,谢谢! 展开
1个回答
2015-02-21
展开全部
<!DOCTYPE HTML>
<html>
<head>
<title>YuGiOh Power Of Chaos</title>
<meta charset="UTF-8" />
<style type="text/css">
table {
margin: auto;
width: 220px;
border: 1px solid black;
border-collapse: collapse;
border: 1px solid black;
}
td {
border: 1px solid black;
}
#check1,#check2 {
width: 100px;
}
</style>
</script>
<script type="text/javascript">
function loadXmlFile(xmlFile)
{
var xmlhttp = null;
if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLDOM");
xmlhttp.async = "false";
xmlhttp.load(xmlFile);
}
else if (document.implementation && document.implementation.createDocument)
{
xmlhttp = new window.XMLHttpRequest();
}
xmlhttp.open("GET", xmlFile, false);
xmlhttp.send(null);
return xmlhttp;
}
var check = function(smt)
{
var TelNo1 = document.getElementById("TelNo1").value;
var TelNo2 = document.getElementById("TelNo2").value;
var check1 = document.getElementById("check1");
var check2 = document.getElementById("check2");
var xmlhttp = loadXmlFile("Check_TELNo1.asp?TelNo1=" + escape(TelNo1) + "&t=" + new Date().getTime());
var msg;
var flag = true;
if (200 == xmlhttp.status)
{
if (xmlhttp.responseText == "yes")
{
msg = "<font color='red'>×</font>";
flag = false;
}
else
{
msg = "√";
}
}
else
{
msg = "网络链接失败";
}
check1.innerHTML = "<font color='#0000ff'>" + msg + "</font>";
xmlhttp = loadXmlFile("Check_TELNo2.asp?TelNo2=" + escape(TelNo2) + "&t=" + new Date().getTime());
if (200 == xmlhttp.status)
{
if (xmlhttp.responseText == "yes")
{
msg = "<font color='red'>×</font>";
flag = false;
}
else
{
msg = "√";
}
}
else
{
msg = "网络链接失败";
}
check2.innerHTML = "<font color='#0000ff'>" + msg + "</font>";
if (!flag)
{
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td><input type="text" id="TelNo1" />
</td>
<td><div id="check1"></div></td>
</tr>
<tr>
<td><input type="text" id="TelNo2" /></td>
<td><div id="check2"></div>
</td>
</tr>
<tr>
<td style="text-align: center;" colspan="2"><input type="submit" value="提交" onclick="return check(this);" /> <input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询