Guest User

Untitled

a guest
Jan 14th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. Ajax/PHP form validation [closed]
  2. <script>
  3. $.validator.setDefaults({
  4. submitHandler: function() { alert("submitted!"); }
  5. });
  6. $(document).ready(function(){
  7. $("#signupForm").validate({
  8. rules: {
  9. firstname:{
  10. required: true,
  11. minlength: 2
  12. },
  13. lastname: "required",
  14. username: {
  15. required: true,
  16. minlength: 2
  17. },
  18. password: {
  19. required: true,
  20. minlength: 5
  21. },
  22. confirm_password: {
  23. required: true,
  24. minlength: 5,
  25. equalTo: "#password"
  26. },
  27. email: {
  28. required: true,
  29. email: true
  30. },
  31. topic: {
  32. required: "#newsletter:checked",
  33. minlength: 2
  34. },
  35. agree: "required"
  36. },
  37. messages: {
  38. firstname:{
  39. required:"Please enter your firstname",
  40. minlength: "Your firstname must be more than 2 characters"
  41. },
  42. lastname: "Please enter your lastname",
  43. username: {
  44. required: "Please enter a username",
  45. minlength: "Your username must consist of at least 2 characters"
  46. },
  47. password: {
  48. required: "Please provide a password",
  49. minlength: "Your password must be at least 5 characters long"
  50. },
  51. confirm_password: {
  52. required: "Please provide a password",
  53. minlength: "Your password must be at least 5 characters long",
  54. equalTo: "Please enter the same password as above"
  55. },
  56. email: "Please enter a valid email address",
  57. agree: "Please accept our policy"
  58. }
  59. })
  60. });
  61.  
  62. (function($) {
  63. $(document).ready(function() {
  64. $('#signupForm').ajaxSubmit(){
  65. $Ajax({
  66. type: 'POST',
  67. url: 'regprocess.php',
  68. dataType: 'json',
  69. success: function(json, textStatus){
  70. $('#signupForm label .error').append(json);
  71. },
  72. error : function(xhr, textStatus, errorThrown){
  73. alert: ('An error occurred! ' + ( errorThrown ? errorThrown : xhr.status )
  74.  
  75. });
  76.  
  77. };
  78.  
  79. });
  80. })
  81. (jQuery);
  82. </script>
  83.  
  84.  
  85. And PHP......
  86.  
  87. <?php
  88. //form processing
  89. //import form info
  90.  
  91. $firstname = $_POST['firstname'];
  92. $lastname = $_POST['lastname'];
  93. $username = $_POST['username'];
  94. $email = $_POST['email'];
  95. $password = $_POST['password'];
  96. $confirm_password = $_POST['confirm_password'];
  97. $myArray[] = $errors;
  98. if(isset($_POST['submit'])) {
  99. function validate($data){
  100. //validate here, return array of errors
  101. if(!isset($data)){
  102. print 'Please enter your ' + $data;
  103. }
  104. else if(strlen($data) <=2){
  105. print 'Your ' + $data + 'must be more than 2 characters';
  106. }else{//data is fine send to encryption
  107. $data = true;
  108. return $data;
  109. };
  110. validate($firstname);
  111. validate($lastname);
  112. validate($username);
  113.  
  114. function is_valid_email_address($email){
  115. $qtext = '[^\x0d\x22\xSc\x80-\xff]';
  116. $dtext = '[^\x0d\x5b-\x5d\x80-\xff]';
  117. $atom = '[^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c' .
  118. '\x3e\x40\x5b-\x5d\x7f-\xff]+';
  119. $quoted_pair = '\x5c[\x00-\x7f]';
  120. $domain_literal = "\x5b($dtext|$quoted_pair)*\x5d";
  121. $quoted_string = "\x22($qtext|$quoted_pair)*\x22";
  122. $domain_ref = $atom;
  123. $sub_domain = "($domain_ref|$domain_literal)";
  124. $word = "($atom|$quoted_string)";
  125. $domain = "$sub_domain(\x2e$word)*";
  126. $local_part = "$word(\x2e$word)*";
  127. $addr_spec = "$local_part\x40$domain";
  128. return preg_match("!^$addr_spec$!", $email)? 1 : 0;
  129. }
  130. if (is_valid_email_address($email)){
  131. print $email + 'is a valid email address';
  132. } else{
  133. print 'Please enter a valid email address';
  134. };
  135.  
  136.  
  137. function validate_password($password){
  138. if(!isset($password)){
  139. print 'Please provide a password';
  140. }else if(strlen($password) <=5){
  141. print 'Your password must be at least 5 charachters long';
  142. }
  143. else{return $password;}
  144. };
  145.  
  146. function confirm_password($confirm_password){
  147.  
  148. if ($password !== $confirm_password){
  149. print 'Please enter the same password as above';
  150. }else{ $password === $confirm_password;}
  151. return $confirm_password;
  152. }
  153. };
  154.  
  155. $ar=array("1"=>one,"2"=>"two");
  156. $json=json_encode($ar);
  157. echo $json;
  158.  
  159. function ajax(calledpage, errordiv)
  160. {
  161. var xmlhttp;
  162. if (window.XMLHttpRequest)
  163. xmlhttp=new XMLHttpRequest();
  164. else
  165. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  166. xmlhttp.onreadystatechange=function(){
  167. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  168. document.getElementById(errordiv).innerHTML=xmlhttp.responseText;
  169. }
  170. xmlhttp.open("GET", calledpage, true);
  171. xmlhttp.send();
  172. }
  173.  
  174. ajax('mypage.php?x=myvariable','my_error_divs_id');
Add Comment
Please, Sign In to add comment