dimanche 25 novembre 2018

how to validate laravel form with jquery AJAX

I am trying to validate laravel form through jquery AJAX.

I have two form fields. One is Email and other is contact. If any one, type email and its already registered. Then it will show "Email id already registred". And same like in contact. I have done it on CORE PHP and it is working. But here it is not working.

my register.blade.php is:

<form method="POST" action="" id="Frm_sign" name="Frm_sign">

<div class="form-group col-md-6 col-sm-6">
<label for="arrival">Contact No*</label>
<input type="text" class="form-control input-sm required digits" id="contact" name="contact" value="" placeholder="">
</div>

<div class="form-group col-md-6 col-sm-6">
<label for="arrival">Email id*</label>
<input type="text" class="form-control input-sm email required" id="email" name="email" value="" placeholder="">
</div>

<div class="col-md-12 col-sm-12">
<div class="form-group col-md-3 col-sm-3 pull-right" >
<input type="submit" class="btn btn-primary" value="Submit" name="submit" />
</div>
</div>
</form>

<!-- form validation -->
<script type="text/javascript">
$(document).ready(function() {
// - validation
if($('#Frm_sign').length > 0){
    $('#Frm_sign').validate( {
    rules:{

     email:{
             remote: {
                    url: "",
                    type: "GET",
                    data: {                                          
                          action: function() { return "1"; },
                          } 
                           }
          },

     contact:{

                   maxlength:10,
                   minlength:10,
                   remote: {
                            url: "",
                            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());
    }


    });

}

});
</script> 

In AJAX, there is password also. But it is working, so I did't write password field here.

My route is:

Route::get('varifyemail','Auth\RegisterController@varifyemail');
Route::get('varifycontact','Auth\RegisterController@varifycontact');

RegisterControlleris:

public function varifyemail(Request $request)
{
     $email1 = User::where('email', $request->email)->get();
    if($email1->email > 0)
    {
        echo json_encode(false);
    }
    else
    echo json_encode(true);
}

public function varifycontact(Request $request)
{
     $contact1 = User::where('contact', $request->contact)->get();
    if($contact1->contact > 0)
    {
        echo json_encode(false);
    }
    else
    echo json_encode(true);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire