Javascript表单验证程序与数学验证码冲突

Javascript表单验证程序与数学验证码冲突,javascript,html,forms,math,captcha,Javascript,Html,Forms,Math,Captcha,这是一个带有数学验证码的基本电子邮件表单。验证码正在工作,但表单验证程序没有工作。当我说FormValidator时,我指的是所需的“名称”和“电子邮件”字段。如果我删除了验证码,表单验证程序就可以正常工作。我认为表单验证程序的js代码和验证码的js代码在某种程度上相互冲突 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transi

这是一个带有数学验证码的基本电子邮件表单。验证码正在工作,但表单验证程序没有工作。当我说FormValidator时,我指的是所需的“名称”和“电子邮件”字段。如果我删除了验证码,表单验证程序就可以正常工作。我认为表单验证程序的js代码和验证码的js代码在某种程度上相互冲突

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>EiP - Contact Us</title>

<script type="text/javascript">
// Form validator for the required Name and Email fields
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
</script>
</head>
<body>  
    <p>Do you have a question or comment about EiP? We want to hear from you as your feedback is critical to our success.</p>
    <p class="required">*Required info</p>
    <form action="contactus.html" method="post" name="contactform" id="contactform" onsubmit="return checkform(this);MM_validateForm('name','','R','email','','RisEmail');return document.MM_returnValue">
    <p class="allforms">Full Name<span>*</span></p>
    <input name="name" type="text"  id="name"/>                        
    <p class="allformsRt">Email Address<span>*</span></p>
    <input type="text" name="email" id="email" />
    <p class="allforms">Company</p>
    <input name="company" type="text"  id="company" />
    <p class="allformsRt">Phone Number</p>
    <input type="text" name="phone" id="phone" />
    <p class="allforms">How can we help?</p>
    <textarea name="application" cols="35" rows="5" title="title" label="label" style="float:left">Type your questions, comments, feedback</textarea>
    <label for="code" style="float:left; clear:both">Write code below > <span id="txtCaptchaDiv" style="color:#F00"></span><!-- this is where the script will place the generated code --> 
    <input type="hidden" id="txtCaptcha" /></label><!-- this is where the script will place a copy of the code for validation: this is a hidden field -->
    <input type="text" name="txtInput" id="txtInput" size="30" style="float:left; clear:both" /> 
    <input name="Submit" type="submit" value="Contact Us" style="float:left; clear:both" />
        </form>

<script type="text/javascript">
//Generates the captcha function    
    var a = Math.ceil(Math.random() * 9)+ '';
    var b = Math.ceil(Math.random() * 9)+ '';       
    var c = Math.ceil(Math.random() * 9)+ '';  
    var d = Math.ceil(Math.random() * 9)+ '';  
    var e = Math.ceil(Math.random() * 9)+ '';  

    var code = a + b + c + d + e;
    document.getElementById("txtCaptcha").value = code;
    document.getElementById("txtCaptchaDiv").innerHTML = code;  
</script>

<script type="text/javascript">
function checkform(theform){
    var why = "";

    if(theform.txtInput.value == ""){
        why += "- Security code should not be empty.\n";
    }
    if(theform.txtInput.value != ""){
        if(ValidCaptcha(theform.txtInput.value) == false){
            why += "- Security code did not match.\n";
        }
    }
    if(why != ""){
        alert(why);
        return false;
    }
}

// Validate the Entered input aganist the generated security code function   
function ValidCaptcha(){
    var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
    var str2 = removeSpaces(document.getElementById('txtInput').value);
    if (str1 == str2){
        return true;    
    }else{
        return false;
    }
}

// Remove the spaces from the entered and generated code
function removeSpaces(string){
    return string.split(' ').join('');
}
</script>
</body>
</html>

EiP-联系我们
//所需名称和电子邮件字段的表单验证程序
函数MM_validateForm(){//v4.0
if(document.getElementById){
变量i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
对于(i=0;i
//生成验证码函数
var a=Math.ceil(Math.random()*9)+'';
var b=Math.ceil(Math.random()*9)+'';
var c=Math.ceil(Math.random()*9)+'';
var d=Math.ceil(Math.random()*9)+'';
var e=Math.ceil(Math.random()*9)+'';
var代码=a+b+c+d+e;
document.getElementById(“txtCaptcha”).value=code;
document.getElementById(“txtCaptchaDiv”).innerHTML=code;
函数检查表(theform){
var为什么=”;
if(theform.txtInput.value==“”){
为什么+=“-安全代码不应为空。\n”;
}
if(theform.txtInput.value!=“”){
if(ValidCaptcha(theform.txtInput.value)==false){
为什么+=“-安全代码不匹配。\n”;
}
}
如果(为什么!=“”){
警惕(为什么);
返回false;
}
}
//根据生成的安全代码函数验证输入的输入
函数ValidCaptcha(){
var str1=removespace(document.getElementById('txtCaptcha').value);
var str2=removespace(document.getElementById('txtInput').value);
如果(str1==str2){
返回true;
}否则{
返回false;
}
}
//从输入和生成的代码中删除空格
函数RemoveSpace(字符串){
返回字符串.split(“”).join(“”);
}

您的
onsubmit
部分不能以这种方式完成。如果安全代码正确,您将返回
undefined
,这不是错误的,因此表单将在调用
MM\u validateForm
函数之前提交,该函数将检查所需字段

从标记中删除
onsubmit=…
部分,并将以下代码添加到站点

<script type="text\javascript">
    window.onload = function() {
        var form = document.getElementsByTagName("form")[0];    // get the form
        form.onsubmit = function() {
            if (checkform(this) != false) {
                MM_validateForm('name', '', 'R', 'email', '', 'RisEmail');
                return document.MM_returnValue;
            } else {
                return false;
            }
        };
    }
</script>

window.onload=函数(){
var form=document.getElementsByTagName(“表单”)[0];//获取表单
form.onsubmit=函数(){
如果(检查表(此)!=错误){
MM_validateForm('name','R','email','RisEmail');
返回文件.MM_返回值;
}否则{
返回false;
}
};
}

wtf?你能重新格式化你的JavaScript代码吗?这样我们就可以“看到”结构而不会感到头痛…很抱歉…我还在学习JavaScript。应该如何重新构造它?验证功能的MM_uu前缀表示你正在使用罐装的Dreamweaver脚本(MM是MacroMedia的遗产).帮你自己一个忙,找一个更简单的验证脚本。这样你会学到更多,而且你有冲突这一事实是远离罐装脚本的一个很好的理由。好的,谢谢你看它和给我的建议。我会研究它,肯定会尝试一下。我总是对学习感到失望!天哪!非常感谢你!我尝试了一个简单的验证脚本正如上面所建议的那样,验证器被替换了,但它仍然不起作用。这非常好用。再次感谢!