Guest User

Untitled

a guest
Nov 28th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. /*
  2. jquery validation support remote verification (return true or false) (you can use that to verify uniqueness for example
  3. */
  4.  
  5. $(function() {
  6. $("#form-register").validate({
  7. rules: {
  8. fname: {
  9. required: true
  10. },
  11. lname: {
  12. required: true
  13. },
  14. pass: {
  15. required: true,
  16. minlength: 10
  17. },
  18. pass_confirm: {
  19. required: true,
  20. minlength: 10,
  21. equalTo: "#pass"
  22. },
  23. cname: {
  24. required: true,
  25. remote: { // <------------------------------------------------------here a remote validation (in this example to check for uniquness otherwise it can be anything)
  26. url: "{{ route('data.unique') }}",
  27. type: "post",
  28. data: {
  29. _token: "{{ csrf_token() }}"
  30. }
  31. }
  32. },
  33. email: {
  34. required: true,
  35. remote: {
  36. url: "{{ route('data.unique') }}",
  37. type: "post",
  38. data: {
  39. _token: "{{ csrf_token() }}"
  40. }
  41. }
  42. }
  43. },
  44. messages: { // <---------- here how to overide the default messages (for different validation types)
  45. fname: {
  46. required: "@lang('auth.validation.first_name_required')"
  47. },
  48. lname: {
  49. required: "@lang('auth.validation.last_name_required')"
  50. },
  51. pass: {
  52. required: "@lang('auth.validation.password_required')",
  53. minlength: "@lang('auth.validation.password_min_len_10')"
  54. },
  55. pass_confirm: {
  56. required: "@lang('auth.validation.password_confirm_required')",
  57. minlength: "@lang('auth.validation.password_min_len_10')",
  58. equalTo: "@lang('auth.validation.password_confirm_non_match')"
  59. },
  60. cname: {
  61. required: "@lang('auth.validation.company_name_required')",
  62. remote: "@lang('auth.validation.company_name_exist')"
  63. },
  64. email: {
  65. required: "@lang('auth.validation.email_required')",
  66. remote: "@lang('auth.validation.email_exist')",
  67. email: "@lang('auth.validation.email_non_valid')"
  68. }
  69. }
  70. });
  71. });
  72.  
  73. /*
  74. Remote : in jquery validation
  75. email: {
  76. required: true,
  77. remote: {
  78. url: "{{ route('data.unique') }}",
  79. type: "post",
  80. data: {
  81. _token: "{{ csrf_token() }}",
  82. }
  83. }
  84. },
  85. */
  86.  
  87. // server side [here php laravel] (the most important thing is that your return true or flase in the end;
  88. public function checkDataUnicity(Request $request){
  89. $toCheck = $this->extractValueToCheck($request->post());
  90. $isUnique = true;
  91. switch ($toCheck) {
  92. case 'cname':
  93. $company = $request->post()[$toCheck];
  94. $isUnique = Company::isUnique($company);
  95. break;
  96.  
  97. case 'email':
  98. $email = $request->post()[$toCheck];
  99. $isUnique = User::isUnique($email);
  100. // $isValidEmail =
  101. break;
  102.  
  103. default:
  104. # code...
  105. break;
  106. }
  107. echo $isUnique ? 'true' : 'false'; // here we are echo true or false (in the response)
  108. }
Add Comment
Please, Sign In to add comment