title: How to Validate a Laravel Form Using jQuery Ajax
body: <p>I'm trying to validate a Laravel form via jQuery/Ajax. I have two form fields: 'email' and 'contact.' If anyone types in email and it's already registered then it will show "Email id already registered". And same like in contact. I have done it in PHP. However, it's not working in the view.</p>
  
  <p><strong>register.blade.php</strong></p>
  
  <pre><code>&lt;form method="POST" action="{{ route('register') }}" id="Frm_sign" name="Frm_sign"&gt;
      {{csrf_field()}}
      &lt;div class="form-group col-md-6 col-sm-6"&gt;
          &lt;label for="arrival"&gt;Contact No*&lt;/label&gt;
          &lt;input type="text" class="form-control input-sm required digits" id="contact" name="contact"
                 value="{{ old('contact') }}" placeholder=""&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group col-md-6 col-sm-6"&gt;
          &lt;label for="arrival"&gt;Email id*&lt;/label&gt;
          &lt;input type="text" class="form-control input-sm email required" id="email" name="email"
                 value="{{ old('email') }}" placeholder=""&gt;
      &lt;/div&gt;
  
      &lt;div class="col-md-12 col-sm-12"&gt;
          &lt;div class="form-group col-md-3 col-sm-3 pull-right"&gt;
              &lt;input type="submit" class="btn btn-primary" value="Submit" name="submit"/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  
  &lt;!-- form validation --&gt;
  &lt;script type="text/javascript"&gt;
      $(document).ready(function () {
  // - validation
          if ($('#Frm_sign').length &gt; 0) {
              $('#Frm_sign').validate({
                  rules: {
                      email: {
                          remote: {
                              url: "{{url("varifyemail")}}",
                              type: "GET",
                              data: {
                                  action: function () {
                                      return "1";
                                  },
                              }
                          }
                      },
                      contact: {
                          maxlength: 10,
                          minlength: 10,
                          remote: {
                              url: "{{url("varifycontact")}}",
                              type: "GET",
                              data: {
                                  action: function () {
                                      return "2";
                                  },
                              }
                          }
                      },
                      password: {
                          equalTo: "#repass"
                      }
                  },
                  messages: {
                      email: {
                          remote: "Email id already registred"
                      },
                      contact: {
                          remote: "Mobile number already registred",
                          maxlength: "Please enter valid mobile number",
                          minlength: "Please enter valid mobile number"
                      },
                      password: {
                          equalTo: "Password is not equal"
                      }
                  },
  
                  submitHandler: function (form) {
  
                      form.submit();
                  },
                  errorPlacement: function (error, element) {
                      error.appendTo(element.parent());
                  }
              });
          }
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>In Ajax, there is password also but it's working, so I didn't write the password field here.</p>
  
  <p><strong>Route</strong></p>
  
  <pre><code>Route::get('varifyemail','Auth\RegisterController@varifyemail');
  Route::get('varifycontact','Auth\RegisterController@varifycontact');
  </code></pre>
  
  <p><strong>RegisterController</strong></p>
  
  <pre><code>public function varifyemail(Request $request)
  {
      $email1 = User::where('email', $request-&gt;email)-&gt;get();
      if($email1-&gt;email &gt; 0)
      {
          echo json_encode(false);
      } else {
          echo json_encode(true);
      }
  }
  
  public function varifycontact(Request $request)
  {
      $contact1 = User::where('contact', $request-&gt;contact)-&gt;get();
      if($contact1-&gt;contact &gt; 0)
      {
          echo json_encode(false);
      } else { 
          echo json_encode(true);
      }
  }
  </code></pre>