2011-03-14 24 views
0

我有一個表單是一個驗證字段,有一個rand()服務器變量顯示在頁面上,用戶必須鍵入該代碼才能完成成功驗證。當用戶改變焦點時,腳本會打開一個php文件,用於檢查用戶輸入的代碼與服務器代碼的有效性。如果代碼匹配,用戶會看到一個提交表單的按鈕,如果輸入的代碼不正確,用戶會收到錯誤消息。使用驗證碼與javascript焦點變化

<script language="javascript"> 

$(document).ready(function() 
{ 
$("#usercode").blur(function() 
{ 
    //remove all the class add the messagebox classes and start fading 
     $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow"); 
    //check the username exists or not from ajax 
    $.post("check.php",{ usercode:$(this).val() } ,function(data) 
    { 
     if(data=='no') //if username not avaiable 
     { 
     $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
     {//alert(data); 
      //add message and change the class of the box and start fading 
      $(this).html('Your code was incorrect').addClass('messageboxerror') .fadeTo(900,1); 
     });  
     } 
     else 
     { 
     $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
     {// alert(data); 
      //add message and change the class of the box and start fading 
      $(this).html('<input type="submit" value="Post Your Story"/>').addClass('messageboxok').fadeTo(900,1);  
     }); 
     } 

    }); 

}); 
}); 
</script> 

/////這是check.php看起來像\\\\

<? 
session_start(); 

$sescode= $_SESSION['code']; 
$usercode = $_POST['user_name']; 

if ($sescode==$usercode) { 
// What happens when valid 
echo("yes"); 
} else { 
// what happens when invalid 
echo "no"; 
} 
?> 

最後是輸入字段

<input type="text" id="usercode" name="usercode" /> 

我需要一種方法來適應這個腳本使用驗證碼驗證工具。我昨天被瘋狂地發送垃圾郵件,並且我想要一種方法來調整此腳本以使用captcha實用程序,我希望在從captcha對象更改焦點後運行驗證腳本。謝謝!

編輯:的一個例子說的形式在這裏可以查看:http://www.mybadhookups.com/ist331.php

+0

希望你仍然在做服務器端驗證。因爲在客戶端隱藏提交按鈕在道路上是微不足道的並且很容易被繞過。 – 2011-03-14 18:02:52

+0

絕對,我也使用spry驗證。 – Ryan 2011-03-14 18:06:05

+0

你使用任何JavaScript庫? jQuery的例子? – 2011-03-14 18:11:07

回答

0

OK,我看你的代碼回答我自己的問題。

一下Keith伍茲jQuery插件驗證碼在這裏:http://keith-wood.name/realPerson.html

他甚至有該選項卡上的服務器端代碼:)

+0

太棒了,但是不是仍然容易受到ocr? – Ryan 2011-03-14 23:15:55

+0

也許有點,但不像以前那樣多。 – 2011-03-15 12:27:33

+0

不錯,謝謝! – Ryan 2011-03-15 14:14:02