使用JQuery和s3captche實(shí)現(xiàn)一個(gè)水果名字的驗(yàn)證
先看個(gè)圖片:
1.介紹:
s3captcha是一個(gè)非常有用的可以讓圖片順序顯示的一個(gè)JQuery插件。它是通過php實(shí)現(xiàn)的。但是我發(fā)現(xiàn)很容易把它轉(zhuǎn)化為asp.net和C#的代碼。 我做了一個(gè)config的配置文件可以在文件中配置圖片的源和名字等。
然后介紹一下s3captcha的實(shí)現(xiàn)原理,
上圖所示是它的實(shí)現(xiàn)模式。
1.它隨即生成圖片的index;
2.把一系列隨機(jī)數(shù)據(jù)賦給圖片的index.
3.可以從圖片列表中選擇一個(gè)隨機(jī)的index.
4.讓圖片隨機(jī)的顯示為一個(gè)radio box.
它使用JQuery實(shí)現(xiàn)的radio box到圖片List的轉(zhuǎn)換。
2.代碼:
首先是把圖片的index數(shù)組順序打亂,重新輸出:
public static List<int> shuffle(List<int> input)
{
List<int> output = new List<int>();
Random rnd = new Random();
int FIndex;
while (input.Count > 0)
{
FIndex = rnd.Next(0, input.Count);
output.Add(input[FIndex]);
input.RemoveAt(FIndex);
}
input.Clear();
input = null;
rnd = null;
return output;
}
使用xml來作為s3captche的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<s3capcha>
<icons>
<name>apple,cherry,lemon,pear,strawberry</name>
<title>Apple,Cherry,Lemon,Pear,Strawberry</title>
<width>33</width>
<height>33</height>
<ext>jpg</ext>
<folder>fruit</folder>
</icons>
<message>Verify that you are a human not robot, please choose {0}</message>
</s3capcha>
GetHtmlCode的代碼:
public static string GetHtmlCodes(string PathTo, out int SessionValue)
{
bool HasValue = false;
if (string.IsNullOrEmpty(Message))
HasValue = LoadConfig();
else
HasValue = true;
if (HasValue)
{
Random Rnd = new Random();
int RandomIndex = Rnd.Next(0,IconNames.Length);
List<int> values = new List<int>();
for(int i = 0; i < IconNames.Length;i++)
values.Add(i);
values = shuffle(values);
string WriteThis = "<div class=\"s3capcha\"><p>" +
string.Format(Message, "<strong>" + IconTitles[values[RandomIndex]] +
"</strong>") + "</p>";
int[] RandomValues = new int[IconNames.Length];
for (int i = 0; i < IconNames.Length; i++)
{
RandomValues[i] = Rnd.Next();
WriteThis += string.Format(RowTemplate,
IconTitles[values[i]], RandomValues[i],
PathTo + "/icons/" + Folder + "/" +
IconNames[values[i]] + "." + Extention,
Width, Height);
}
WriteThis += "<div style="\" style="\""clear:left\"></div></div>";
SessionValue = RandomValues[RandomIndex];
return WriteThis;
}
else
{
SessionValue = -1;
return "Invalid data, config file not found";
}
}
3.使用ajax方法來實(shí)現(xiàn)驗(yàn)證信息的判斷彈出框:
s3capcha.ashx 用來實(shí)現(xiàn)當(dāng)向服務(wù)器請(qǐng)求時(shí),返回html:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
int USession;
context.Response.Write(s3capcha.GetHtmlCodes("../../s3capcha", out USession));
context.Session[s3capcha.s3name] = USession;
context.Response.End();
}
verify.ashx文件·來實(shí)現(xiàn)驗(yàn)證功能:
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (s3capcha.Verify(context.Session[s3capcha.s3name],
context.Request.Form[s3capcha.s3name]))
context.Response.Write("Success");
else
context.Response.Write("Fail");
context.Response.End();
}
JQuery實(shí)現(xiàn)的ajax代碼:
//Javascript codes
$(document).ready(function() {
getCapcha();
$("form").bind('submit', function() {
$.ajax({
url: 'verify.ashx',
type: 'POST',
data: { 's3capcha': $("input[name=s3capcha]:checked").val() },
cache: false,
success: function(data) {
alert(data);
getCapcha();
}
});
return false;
});
});
相關(guān)文章
jQuery學(xué)習(xí)筆記之Ajax用法實(shí)例詳解
這篇文章主要介紹了jQuery學(xué)習(xí)筆記之Ajax用法,結(jié)合實(shí)例形式較為詳細(xì)的分析總結(jié)了jQuery中ajax的相關(guān)使用技巧,包括ajax請(qǐng)求、載入、處理、傳遞等,需要的朋友可以參考下2015-12-122014年50個(gè)程序員最適用的免費(fèi)JQuery插件
這篇文章主要介紹了2014年50個(gè)程序員最適用的免費(fèi)JQuery插件,需要的朋友可以參考下2014-12-12詳談jQuery中使用attr(), prop(), val()獲取value的異同
下面小編就為大家?guī)硪黄斦刯Query中使用attr(), prop(), val()獲取value的異同。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04jquery獲取table中的某行全部td的內(nèi)容方法
jquery獲取table中的某行全部td的內(nèi)容方法,需要的朋友可以參考一下2013-03-03基于jQuery實(shí)現(xiàn)掛號(hào)平臺(tái)首頁(yè)源碼
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)掛號(hào)平臺(tái)首頁(yè)源碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Jquery刷新頁(yè)面背景圖片隨機(jī)變換的實(shí)現(xiàn)方法
Jquery刷新頁(yè)面背景圖片隨機(jī)變換的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-03-03詳解Jquery 遍歷數(shù)組之$().each方法與$.each()方法介紹
這篇文章主要介紹了詳解Jquery 遍歷數(shù)組之$().each方法與$.each()方法介紹 ,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01jQuery實(shí)現(xiàn)的簡(jiǎn)單對(duì)話框拖動(dòng)功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的簡(jiǎn)單對(duì)話框拖動(dòng)功能,涉及jQuery事件響應(yīng)、數(shù)值運(yùn)算及頁(yè)面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06jQuery實(shí)現(xiàn)鼠標(biāo)滑過圖片移動(dòng)特效
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)滑過圖片移動(dòng)特效,鼠標(biāo)移動(dòng)到圖片上時(shí)圖片向上動(dòng)一下,等到鼠標(biāo)離開后,圖片又返回到原來位置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框效果(2)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02