2014-01-06 139 views
0

我正在嘗試使用Jquery Validate插件來驗證我的驗證碼是PHP。我GOOGLE了這個,但似乎無法找到和答案讓我的代碼工作。使用jQuery驗證PHP驗證碼

我貼我下面的代碼是相關的問題,下面

HTML:

<img src="Captcha/captcha.php" name="captcha_img" id="captcha_img" /> 
     <br/> 
     <input style="width:150px; text-align:center;" type="text" id="answer" name="answer" placeholder="Enter captcha here" /> 
     <br/> 
     <input style="width:150px; background-color:#B9C6F0; font-size:10px; height:15px;" type="button" onclick="document.getElementById('captcha_img').src='Captcha/captcha.php?img=' + Math.random(); return false" value="Refresh Captcha" /> 
下面

JS:

<script src="JS/jquery.validate.js"></script> 
<script src="JS/additional-methods.min.js"></script> 

    <script> 
    $(document).ready(function() { 

     $("#SignUp").validate({ 
      onkeyup: function(element) { $(element).valid(); }, 
      rules: { 
       email: { 
        required: true, 
        email: true 
       }, 
       password: { 
        required: true, 
        password: true 
       }, 
       confirm_password: { 
        equalTo: "#password", 
        required: true 
       }, 
       company: { 
        nls:true, 
        required: true 
       }, 
       telephone: { 
        required: true, 
        phoneUK: true 
       }, 
       email2: { 
        email: true 
       }, 
       website: { 
        url: true 
       }, 
       address1: { 
        nls:true 
       }, 
        address2: { 
        nls:true 
       }, 
       town: { 
        nls:true  
       }, 
       postcode: { 
        postcodeUK:true 
       }, 
       country: { 
        selectcountry:true 
       }, 
       terms:{ 
        required:true    
       }, 
       answer:{ 
        required: true, 
        remote: "Captcha/process.php" 

       } 

      }, 
      messages:{ 
       email: 
         { 
          required: "Please Enter an Email Address" 
         }, 
       password: 
         { 
          required: "Please Enter a Password" 
         }, 
       confirm_password: 
         { 
          required: "Please Confirm Your Password" 
         }, 
       company: 
         { 
          required: "Please Enter Your Company/Climbing Gym Name" 
         }, 
       telephone: 
         { 
          required: "Please Enter a Telephone Number" 
         }, 
       terms: 
         { 
          required: "Please Agree Our Terms and Conditions" 
         }, 
       answer:{ 
          required: "Please Enter The Captcha Code", 
          remote: "Captcha Entered Incorrectly" 
         } 

      } 
     }); 

     $.validator.addMethod("password", function (value, element) { 
      return this.optional(element) || /^[[email protected]#$%^&*()_]{6,16}$/i.test(value); 
     }, "Passwords are 6-16 characters"); 

     $.validator.addMethod("nls", function(value, element) 
     { 
     return this.optional(element) || /^[a-zA-Z0-9\s.\-_']+$/i.test(value); 
     }, "Please Only Enter Alpha Numeric Characters and Spaces"); 


     jQuery.validator.addMethod('selectcountry', function (value) { 
      return (value != 'Nothing'); 
     }, "Please Select a Country"); 

     $.validator.addMethod("url", function(value, element) 
     { 
     return this.optional(element) || /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i.test(value); 
     }, "Please Enter A Valid Website URL"); 


    }); 
    </script> 

PHP下面:

Process.php

<?php 

if(strtolower($_POST['answer']) == $_SESSION['captcha']){ 
    echo 'true'; 
    }else{ 
    echo 'false'; 
    } 

unset($_SESSION['captcha']); 


?> 

Captcha.php

<?php 
/* captcha.php file*/ 

    session_start(); 

    header("Expires: Tue, 01 Jan 2013 00:00:00 GMT"); 
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
    header("Cache-Control: no-store, no-cache, must-revalidate"); 
    header("Cache-Control: post-check=0, pre-check=0", false); 
    header("Pragma: no-cache"); 

    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $randomString = ''; 

    for ($i = 0; $i < 5; $i++) 
    { 
     $randomString .= $chars[rand(0, strlen($chars)-1)]; 
    } 

    $_SESSION['captcha'] = strtolower($randomString); 


    $im = @imagecreatefrompng("captcha-background-rollcode.png"); 


    imagettftext($im, 30, 0, 10, 38, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $randomString); 

    header ('Content-type: image/png'); 
    imagepng($im, NULL, 0); 
    imagedestroy($im); 

?> 

回答

0

添加規則

 
answer: {remote: "check-captcha.php" } 
在驗證和創建文件 check-captcha.php
if(strtolower($_REQUEST['answer']) == $_SESSION['captcha']) echo '1'; 
else '0'; 
請檢查 $_REQUEST array的精確索引(也可能是其他比 answer

+0

這似乎沒有工作,不知道如果在某些時候我必須讓jquery驗證連接到驗證碼生成的Captcha.php? – user3166610

+0

你上面試過了嗎? –

+0

是的,不知道我去哪裏錯了 – user3166610