Captcha Validator


/ Published in: JavaScript
Save to your folder(s)

captcha validator


Copy this code and paste it in your HTML
  1. /*
  2. *
  3. * Captcha Validator 1.0
  4. *
  5. * This script is distributed under the GNU Lesser General Public License.
  6. * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
  7. *
  8. * Copyright (C) 2010 HTML Form Guide
  9. * http://www.html-form-guide.com/
  10. */
  11.  
  12. function FG_CaptchaValidator(captcha_ip,captcha_img)
  13. {
  14. this.captcha_ip = captcha_ip;
  15.  
  16. this.captcha_img = captcha_img;
  17.  
  18. this.validatedCode=''
  19.  
  20. this.validate = function()
  21. {
  22. if(this.validatedCode.length==0 ||
  23. this.validatedCode != this.captcha_ip.value)
  24. {
  25. this.ValidateOnline();
  26. return false;
  27. }
  28. else
  29. {
  30. return true;
  31. }
  32. }
  33.  
  34. this.OnSuccess = function()
  35. {
  36. var msg = this.GetResponseText();
  37. if(msg == 'success')
  38. {
  39. this.validatedCode = this.captcha_ip.value;
  40. if(this.captcha_ip.form.onsubmit())
  41. {
  42. this.captcha_ip.form.submit();
  43. }
  44. }
  45. else
  46. {
  47. sfm_show_error_msg(msg,this.captcha_ip);
  48. document.error_disp_handler.FinalShowMsg();
  49. }
  50.  
  51. }
  52.  
  53. this.ValidateOnline = function()
  54. {
  55. var url = captcha_img.src;
  56. var postStr = this.captcha_ip.name + "=" +
  57. encodeURIComponent( this.captcha_ip.value )+'&fg_validate_captcha=y';
  58.  
  59. this.Init('POST', url);
  60.  
  61. this.Send(postStr);
  62. }
  63. }
  64.  
  65. FG_CaptchaValidator.prototype = new FG_Ajax();
  66.  
  67. function FG_Ajax()
  68. {
  69. var _request = null;
  70. var _this = null;
  71.  
  72. this.Init = function(method, url)
  73. {
  74. _Init();
  75. _this = this;
  76.  
  77. switch (arguments.length)
  78. {
  79. case 2:
  80. _request.open(method, url);
  81. break;
  82.  
  83. case 3:
  84. _request.open(method, url, arguments[2]);
  85. break;
  86. }
  87.  
  88. if (arguments.length >= 4)
  89. {
  90. _request.open(method, url, arguments[2], arguments[3]);
  91. }
  92.  
  93. this.SetRequestHeader("Content-Type",
  94. "application/x-www-form-urlencoded; charset=UTF-8");
  95. }
  96.  
  97. this.SetRequestHeader = function(field, value)
  98. {
  99. if (_request)
  100. {
  101. _request.setRequestHeader(field, value)
  102. };
  103. }
  104.  
  105. this.Send = function(data)
  106. {
  107. if (_request){ _request.send(data)};
  108. }
  109.  
  110. this.GetResponseText = function()
  111. {
  112. return (_request) ? _request.responseText : null;
  113. }
  114.  
  115. this.OnSuccess = function() { };
  116. this.OnFailure = function() { };
  117.  
  118. function _OnSuccess()
  119. {
  120. _this.OnSuccess();
  121. }
  122.  
  123. function _OnFailure()
  124. {
  125. _this.OnFailure();
  126. }
  127.  
  128. function _Init()
  129. {
  130. _request = _GetXmlHttpRequestObject();
  131. if(_request)
  132. {
  133. _request.onreadystatechange = _StateHandler;
  134. }
  135. }
  136.  
  137. function _StateHandler()
  138. {
  139. if(4 == _request.readyState )
  140. {
  141. if (_request.status == 200)
  142. {
  143. _OnSuccess();
  144. }
  145. else
  146. {
  147. _OnFailure();
  148. }
  149. }
  150. }
  151.  
  152. function _GetXmlHttpRequestObject()
  153. {
  154. if (window.XMLHttpRequest)
  155. {
  156. return new XMLHttpRequest(); //Mozilla, Safari ...
  157. }
  158. else
  159. if (window.ActiveXObject)
  160. {
  161. return new ActiveXObject("Microsoft.XMLHTTP"); //IE
  162. }
  163. else
  164. {
  165. return null;
  166. }
  167. }
  168. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.