Guest User

Untitled

a guest
Aug 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. Jquery and ajax, my function for username checking?
  2. function check_username() {
  3. var username = $("#username").val();
  4. $('.loading').fadeIn().delay(100);
  5. $.post("ajax.php", {
  6. username: $('#username').val(),
  7. }, function (response) {
  8. $('.error, .success').hide();
  9. setTimeout(function () {
  10. $('.loading').hide();
  11. finishAjax('username', response);
  12. }, 1000);
  13. });
  14. return false;
  15. }
  16.  
  17. function finishAjax(id, response) {
  18. $('#' + id).after(response).fadeIn(1000);
  19. }
  20.  
  21. <?php
  22.  
  23. require('dbc.php');
  24.  
  25. if (isset($_REQUEST['username'])) {
  26.  
  27. $q = $dbc -> prepare("SELECT username FROM accounts WHERE username = ?");
  28. $q -> execute(array($_REQUEST['username']));
  29.  
  30. if (strlen($_REQUEST['username']) < 3) {
  31. echo '<div class="error">Has to be at least 3 characters</div>';
  32. }
  33. elseif ($q -> rowCount() > 0) {
  34. echo '<div class="error">Username already taken</div>';
  35. }
  36. else {
  37. echo '<div class="success">Username available</div>';
  38. }
  39. }
  40.  
  41. ?>
  42.  
  43. $.fn.checkValid = function(options)
  44. {
  45. var response = function(response) {
  46. var setClass = '';
  47. var $span = $(this).data('checkValidTip');
  48. if ($span)
  49. {
  50. $span.remove();
  51. }
  52.  
  53. if (response === undefined) return;
  54.  
  55. setClass = (response.valid ? 'valid' : 'invalid');
  56.  
  57. var $span = $('<span>' + response.msg + '</span>');
  58. $(this)
  59. .data('checkValidTip', $span)
  60. .after($span);
  61.  
  62. $span.hide()
  63. .fadeIn(1000)[0]
  64. .className = setClass;
  65. };
  66.  
  67.  
  68. var ajaxOptions = {
  69. type: 'GET',
  70. url: 'ajax.php',
  71. success: response,
  72. dataType: 'json'
  73. };
  74.  
  75. this.each(function() {
  76.  
  77. var that = this;
  78. var ajaxRequest = ajaxOptions;
  79. ajaxRequest.data = {};
  80. ajaxRequest.data[options.key] = this.value;
  81. ajaxRequest.context = that
  82.  
  83. $.ajax(ajaxRequest);
  84. });
  85. };
  86.  
  87. $('#username, #email').blur(function() {
  88. $(this).checkValid({ key: this.id });
  89. });
  90.  
  91. <?php
  92. // Do your sql statements here, decide if input is valid or not
  93.  
  94. $arr = array('valid' => $is_valid,
  95. 'msg' => $error_or_good_msg
  96. );
  97.  
  98. echo json_encode($arr);
  99.  
  100. /* For example will output:
  101. {
  102. "valid": "false",
  103. "msg": "<b>Error: Must be at least 2 characters</b>"
  104. }
  105. Which can be read directly as response.valid
  106. or response.msg from within response() function
  107. */
  108.  
  109. var validations = {
  110. username: {
  111. min_chars: 4,
  112. max_chars: 10,
  113. valid_chars: 'qwertyuiopasdfghjklzxcvbnm_-'
  114. },
  115.  
  116. email: {
  117. regex: /./ //your magic regex here
  118. }
  119. };
Add Comment
Please, Sign In to add comment