Guest User

Untitled

a guest
Aug 1st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. If/else statements in PHP and Ajax input validation
  2. if(isset($_POST['username']) && isset($_POST['email']) && isset($_POST['email2']) && isset($_POST['password'])
  3. && isset($_POST['firstname']) && isset($_POST['surname']) && isset($_POST['gender']) && isset($_POST['day'])
  4. && isset($_POST['month']) && isset($_POST['year']) ) {
  5.  
  6. $username = $_POST['username'];
  7. $email = $_POST['email'];
  8. $email2 = $_POST['email2'];
  9. $password = $_POST['password'];
  10. $firstname = $_POST['firstname'];
  11. $surname = $_POST['surname'];
  12. $gender = $_POST['gender'];
  13. $day = $_POST['day'];
  14. $month = $_POST['month'];
  15. $year = $_POST['year'];
  16.  
  17.  
  18. if(!preg_match("/^[a-z](?=[w.]{3,19}$)w*.?w*$/i",$username)){
  19.  
  20. echo "not a valid username.";
  21. }
  22. else if(filter_var($email,FILTER_VALIDATE_EMAIL)){
  23.  
  24. echo "OK!";
  25. }
  26. else if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
  27.  
  28. echo "not a valid email address";
  29. }
  30. else if(strcmp($email,$email2) != 0){
  31.  
  32. echo "emails are different.";
  33. }
  34. else if(strcmp($email,$email2) == 0){
  35.  
  36. echo "OK!";
  37. }
  38. else if(!preg_match("[a-zA-Z]*",$firstname)){
  39.  
  40. echo "Not a valid firstname.";
  41.  
  42. }
  43. else if(preg_match("[a-zA-Z]*",$firstname)){
  44.  
  45. echo "OK!";
  46.  
  47. }
  48. else if(!preg_match("[a-zA-Z]*",$surname)){
  49.  
  50. echo "not a valid surname.";
  51.  
  52. }
  53. else if(preg_match("[a-zA-Z]*",$surname)){
  54.  
  55. echo "OK!";
  56. }
  57. }
  58.  
  59. function handlePost() {
  60.  
  61. var username = $('#username').val();
  62. var email = $('#email').val();
  63. var email2 = $('#email2').val();
  64. var password = $('#password').val();
  65. var firstname = $('#firstname').val();
  66. var surname = $('#surname').val();
  67. var gender = $('#gender').val();
  68. var day = $('#day').val();
  69. var month = convertMonth($('#month').val())
  70. var year = $('#year').val();
  71.  
  72. $.ajax({
  73. type: "POST",
  74. url: "handleRegister.php",
  75. data: "username="+username+"&email="+email+"&email2="+email2+"&password="+password+"&firstname="
  76. +firstname+"&surname="+surname+"&gender="+gender+"&day="+day+"&month="+month+"&year="+year,
  77. success: function(resp){
  78. // we have the response
  79. //alert("Server said:n '" + resp + "'");
  80.  
  81. console.log("Server said:n '" + resp + "'")
  82. },
  83. error: function(e){
  84. //alert('Error: ' + e);
  85. console.log("Server said:n '" + e + "'")
  86. }
  87. });
  88. }
  89.  
  90. $errors = array();
  91.  
  92. if (!preg_match("/^[a-z](?=[w.]{3,19}$)w*.?w*$/i", $username))
  93. {
  94. $errors[] = "not a valid username.";
  95. }
  96. if (!filter_var($email, FILTER_VALIDATE_EMAIL))
  97. {
  98. $errors[] = "not a valid email address";
  99. }
  100. if ($email !== $email2)
  101. {
  102. $errors[] = "emails are different";
  103. }
  104. if (!preg_match("[a-zA-Z]*", $firstname))
  105. {
  106. $errors[] = "Not a valid firstname.";
  107. }
  108. if (!preg_match("[a-zA-Z]*", $surname))
  109. {
  110. $errors[] = "not a valid surname.";
  111. }
  112.  
  113. if ($errors)
  114. {
  115. echo implode("n", $errors);
  116. }
  117. else
  118. {
  119. echo 'OK!';
  120. }
  121.  
  122. if(!preg_match("/^[a-z](?=[w.]{3,19}$)w*.?w*$/i",$username)){
  123.  
  124. echo "not a valid username.";
  125. }
  126. if(filter_var($email,FILTER_VALIDATE_EMAIL)){
  127.  
  128. echo "OK!";
  129. }
  130. if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
  131.  
  132. echo "not a valid email address";
  133. }
  134. if(strcmp($email,$email2) != 0){
  135.  
  136. echo "emails are different.";
  137. }
  138. etc....
Add Comment
Please, Sign In to add comment