我使用JavaScript验证验证码不起作用

时间:2017-02-08 07:42:39

标签: javascript

    <script>
    function validate2(id)
    {
        var regex = [a-z];
        var ctrl =  document.getElemetnById(id);

        if (regex.test(ctrl.value)) {
            return true;
        }
        else {
            return false;
        }
    }
    </script>

    <script>

    function TestCompanyName(txtCompanyName){

        var obj = document.getElementById(txtCompanyName);

        var RegEx = /THE DAMN REGULAR EXPRESSION/

        if(RegEx.test(obj.value)==false)

        {

            alert("Invalid");

        }

    }

    </script>
    </script>
    <script>
    function checklname(input1)
    {
        var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        return pattern.test(input1);
    }
    if(!isvalid) {
        alert('Invalid name');
        document.getElementById("input1").value = "";
    }
    }
    </script>
    <script>
    function phonenumber(telno) {
      var phoneno = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
      if(telno.value.match(phoneno))
          {
        return true;
      }  
      else {  
        alert("message");
        return false;
      }
    }

    </script>

    <script>
    function phonenumber2(mobileno) {
      var phoneno1 = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
      if(mobileno.value.match(phoneno1))
          {
        return true;
      }  
      else {  
        alert("message");
        return false;
      }
    }

    </script>
        <script>

    function validate() {
        var ta = document.getElementById("ta").value;
        var answer  = document.getElementbyId("answer").value;
        var digit1 = parseInt(document.getElementById("digit1").innerHTML);
        var digit2 = parseInt(document.getElementById("digit2").innerHTML);
        var sum = digit1 + digit2;
        if(answer == null || answer == ""){
            alert("please add the number");
            return false;
        }else if(answer != sum){
            alert("you math is wrong");
        }else if(ta == null || ta == ""){
            alert("please fill in  the textarea");
        }else{
            document.getElementById("status").innerHTML = "processing";
            document.getElementById("answer").innerHTML = "";
        }
        }


    function randomNums(){
        var rand_num1 = Math.floor(Math.random() * 10) +1;
        var rand_num2 = Math.floor(Math.random() * 10) +1;
        document.getElementById("digit1").innerHTML = rand_num1;
            document.getElementById("digit2").innerHTML = rand_num2;
    }
    </script








    <script>
    function checkEmail(inputvalue){    
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
             return pattern.test(inputvalue); 
    }
    checkEmail('rte@co') // false
    checkEmail('rte@co.com') // true
    </script>
    <script>
    var address = /^[a-zA-Z0-9-\/] ?([a-zA-Z0-9-\/]|[a-zA-Z0-9-\/] )*[a-zA-Z0-9-\/]$/;
                if ( address.test($.trim($('#address').val())) == false)
                {
                     alert('invalid address ');
                }
            </script>
    <script>
    function IsValidZipCode(zipcode) {
            var isValid = /[\^$%@!#&\*:<>\?\/\\~\{\}\(\)\+|]/.test(zipcode);
            if (!isValid){
           alert('Invalid ZipCode');
          document.getElementById("zipcode").value = "";
        }


    </script>



    <script type="text/javascript" src="js/bootstrap-3.1.1.min.js"></script>
            <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
            <?php include("include files/favicon.php"); ?>
        </head>
        <body itemscope itemtype="http://schema.org/Organization">
            <?php 
            include("include files/header.php");
            ?>
            <?php 
            include("include files/navigation.php");
            ?>
            <!--breadcrumb-->
            <div id='location'>
            <div id="BannerAndNavigatorHtmlBlock_StoreNavigator_pnNavigator" itemscope=""  itemtype="http://schema.org/BreadcrumbList" class="btn-group btn-breadcrumb">
            <span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem" class="btn btn-success">
            <a itemprop="item" href="/">
            <span class="glyphicon glyphicon-home" itemprop="name">
            </span>
            </a>
            <span itemprop="position" content="1">
            </span>
            </span>
            </span>
            <span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"class="btn btn-success">
            <span itemprop="name">Playing Card Quote</span><span itemprop="position" content="2"></span>
            </span>
            </div>
            </div>
            <!--Main Content-->
            <div class="wrapper">
            <div class="container">
            <div>&nbsp;</div>
            <div class="well">
            <form action="thank-you.php" method="post" id="form1" onsubmit="MM_validateForm('quantity','','R','fname','','R','email','','NisEmail','telno','','RisNum','address','','R','city','','R','state','','R','country','','R','zipcode','','R');return document.MM_returnValue && jcap();">
            <fieldset>
            <legend>
        <h1>Fill Quote Form</h1>
    </legend>
    <div class="quote-form">
        <div>&nbsp;</div>
        <div class="row">
            <div class="form-group">
                <label class="control-label col-sm-6">Plastic Coated Paper :</label>
                <div class="col-sm-3">
                    <select class="form-control" name="plastic_coated_paper" id="plastic_coated_paper" onchange="chgSelect('coatedpaper');">
                        <option selected value="0" id="selectpaper">Select Paper</option>
                        <option>Black Centered 330</option>
                        <option>Black Centered 320</option>
                        <option>Black Centered 315</option>
                        <option>Black Centered 305</option>
                        <option>Black Centered 300</option>
                        <option>Black Centered 280</option>
                        <option>White Centered 330</option>
                        <option>White Centered 320</option>
                        <option>White Centered 315</option>
                        <option>White Centered 305</option>
                        <option>White Centered 300</option>
                        <option>White Centered 280</option>
                    </select>
                </div>
                <div class="col-sm-3" style="text-align:center;">
                </div>
                <br>
                <br>
                          <center>OR</center>
            </div>
        </div>
        <div>&nbsp;</div>
        <div class="row">
            <div class="form-group">
                <label class="control-label col-sm-6">100% Pure Plastic : </label>
                <h2>Your Contact Information :</h2>
                <form action="" method="POST">
                <div>&nbsp;</div>
                      <div class="row">
                    <div class="form-group">
                    <label class="control-label col-sm-6"><i>*</i> First Name</label>
                    <div class="col-sm-3">
                    <div>
                   <input  name="fname" id= "id" type="text" onSubmit="" tabindex="2" required="required">
                    </div>
                    </div>
                    <div class="col-sm-3">
                    </div>
                    </div>
                    </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Last  Name</label>
                <div class="col-sm-3">
                <div>
            <label>
        <input name="lname" id="input1" type="text" tabindex="2" required="required">
                </label>
            </div>
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Email Id</label>
                <div class="col-sm-3">
                 <div>
                <label>
                 <input name="email" type="email" tabindex="2" required="required">
                </label>
                 </div>
                 </div> 
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <div class="col-sm-6">
                </div>
                <div class="col-sm-5">(Please type in a correct email address , as the quotes will be forwarded there)</div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Telephone Number</label>
                <div class="col-sm-3">
                <div>
                <label>
                <input  name="telno" id="telno" type="text" tabindex="2" required="required">
                </label>
                 </div>
                </div>
                <div class="col-sm-3">( Do not enter space)
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Mobile Number</label>
                <div class="col-sm-3">
                <div>
                <label>
               <input  name="mobileno" id="mobileno" type="text" tabindex="2" required="required">
                </label>
                 </div>
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6">&nbsp;<i>*</i> Company Name</label>
                <div class="col-sm-3">
               <div>
                <label>
            <input type="text" name="txtCompanyName" id="txtCompanyName" required="required" onclick="TestCompanyName('txtCompanyName')">
         </label>
            </div>
            </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Address</label>
                <div class="col-sm-3">
                <input id="address" class="address" type="text"   name="address" 
                onchange="IsValidAddress(this.form.address.value)" required="required" >     
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Zip Code :</label>
                <div class="col-sm-3">
       <input id="zipcode" class="zipcode" type="text"  name="zipcode" onchange="IsValidZipCode(this.form.zipcode.value)" required="required" >
    <br />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div> 
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> City</label>
                <div class="col-sm-3">
                <input class="form-control" name="city" type="text" id="city" required="required" value="">   
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> State</label>
                <div class="col-sm-3">
                <input class="form-control" name="state" type="text" id="state" value="" required ="required">     
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Country</label>
                <div class="col-sm-3">
                <input class="form-control" name="country" type="text" id="country" value="" required="required">    
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6">&nbsp;Fax</label>
                <div class="col-sm-3">
                <input class="form-control" name="fax" type="text" id="fax" value="" required ="required">    
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Captcha</label>
            <div class="col-sm-2">
            </div>
            <div class="col-sm-3">
        <body>
    <form name="review" ACTION="newpg.html" METHOD="POST" onsubmit="return checkform(this);">
    <font color="#DD0000">Enter Code ></font> <span id="txtCaptchaDiv" style="background-color:#A51D22;color:#FFF;padding:5px"></span>
    <input type="hidden" id="txtCaptcha" />
    <input type="text" name="txtInput" id="txtInput" size="15" />
    <input type="submit" value="Submit"/>
    </form>

    <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;
    }
    }

    //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;

    // 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>
    <div class="col-sm-3">
            </div>
            </div>
            <div>&nbsp;</div>
            <div class="row">
            <div class="form-group">
            <div class="col-sm-12">
            <center>
            <input type="submit" value="Submit" class="btn1" name="submit" id="send">
            </center>
            </div>
            </div>
            </div>
            <div>&nbsp;</div>
            </div>
            </fieldset>
            </form>
            </div>
            </div>
            </div>
            <!---footer---><?php include("include files/footer.php");?>
        </body>
    </html>

我无法使用验证为验证码提供验证?如果安全代码不匹配或安全代码不应为空,我的函数不会显示消息。

该计划有什么问题?为什么不正确验证我的验证码?我尝试了很多次,但无法验证我的验证码

验证码验证页面中的错误是什么?验证码的正确验证是什么?

2 个答案:

答案 0 :(得分:1)

您发布的代码没有任何问题。在允许提交表单之前,它会执行它应该执行的操作,生成代码并验证输入是否匹配。

但是,如果您真的对阻止机器人提交表单感兴趣,那么此代码根本不会对您有所帮助。您在客户端上生成代码并将其保存在输入中。机器人会读取该输入并为验证码提供任何问题。

编辑:重新开始的建议

我建议您将应用程序与已建立的重新提供者集​​成,例如recaptcha。请注意,验证码输入的验证应在服务器中执行 您可以在recaptcha here找到更多详细信息。

答案 1 :(得分:0)

代码正如您所料。请回顾一下我不得不改变的小提琴。 https://jsfiddle.net/43m63ezf/

<强> HTML          

<label>Captcha</label>
<form name="review" ACTION="palying-cards-quote.php" METHOD="POST" onsubmit="return checkform(this);">
  <font color="#DD0000">Enter Code </font> <span id="txtCaptchaDiv" style="background-color:#A51D22;color:#FFF;padding:5px"></span>
  <input type="hidden" id="txtCaptcha" />
  <input type="text" name="txtInput" id="txtInput" size="15" />
  <input type="submit" value="Submit" />
</form>

<强> JS

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;
  }
}

//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;

// 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('');
}