javascript - Jquery form validation before Ajax request - Stack Overflow

I have gone through other threads and used one for reference but still I am not able to find the soluti

I have gone through other threads and used one for reference but still I am not able to find the solution.
My question is:

I am calling a function on button click now that function has validation after validation I am trying to post data with ajax request in submit handler, problem is my fields are getting verified but Ajax request is not invoked.

<input type="button" value="Register" id="registerP" onclick="registerPatient()"  class="form-control input-sm btn-success ">

function registerPatient(){
$("#patientRegistration").validate({
rules: {
    patientName: {
    required: true,
    textOnly: true
                },
       },
messages: {
       patientName: {
       required: "Please enter the full name",                  
                    },
          },
submitHandler: function(form) { 
            $.ajax({
             type: "POST",
             url: "/LoginMavenSpringMVC/appointment/savePatient",
             data: "patientName=" + patientName,
         success: function(response){},
         error: function(e){}
      });
  }
 });
}  

However if I am not using validation and calling Ajax directly i am able to post the data. Please suggest where I am going Wrong.

I have gone through other threads and used one for reference but still I am not able to find the solution.
My question is:

I am calling a function on button click now that function has validation after validation I am trying to post data with ajax request in submit handler, problem is my fields are getting verified but Ajax request is not invoked.

<input type="button" value="Register" id="registerP" onclick="registerPatient()"  class="form-control input-sm btn-success ">

function registerPatient(){
$("#patientRegistration").validate({
rules: {
    patientName: {
    required: true,
    textOnly: true
                },
       },
messages: {
       patientName: {
       required: "Please enter the full name",                  
                    },
          },
submitHandler: function(form) { 
            $.ajax({
             type: "POST",
             url: "/LoginMavenSpringMVC/appointment/savePatient",
             data: "patientName=" + patientName,
         success: function(response){},
         error: function(e){}
      });
  }
 });
}  

However if I am not using validation and calling Ajax directly i am able to post the data. Please suggest where I am going Wrong.

Share Improve this question edited Dec 28, 2016 at 7:52 Alexis 5,8311 gold badge29 silver badges46 bronze badges asked Dec 28, 2016 at 7:39 Vipul SinghVipul Singh 3931 gold badge10 silver badges26 bronze badges 2
  • give url or upload plete , so that can clearly look into – Ganesh Putta Commented Dec 28, 2016 at 7:45
  • i dint get your point. – Vipul Singh Commented Dec 28, 2016 at 8:12
Add a ment  | 

2 Answers 2

Reset to default 5

You can try like this which call jquery form validation and check if validated:

$(document).ready(function(){
  $("#patientRegistration").validate({
    rules: {
        patientName: {
        required: true,
        textOnly: true
                    },
           },
    messages: {
           patientName: {
           required: "Please enter the full name",                  
                        },
              }
     });
});

function registerPatient(){
   var IsValid=$("#patientRegistration").valid();
   if(IsValid){
     var patientName=""; //value for patient name
     $.ajax({
             type: "POST",
             url: "/LoginMavenSpringMVC/appointment/savePatient",
             data: {"patientName": patientName},
             success: function(response){},
             error: function(e){}
     });
   }
}

Change your data syntax of ajax to this.

data: {patientName:patientName},

Make sure you have a parameter on catching method of the same name "patientName" to invoke its post from page on post.

This should work.

Also check if you get a patientName paramater value in your post. To do so first check by "alert" and pass the patientName parameter. You will know what you are getting and why post is not happening.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745079347a4610027.html

相关推荐

  • javascript - Jquery form validation before Ajax request - Stack Overflow

    I have gone through other threads and used one for reference but still I am not able to find the soluti

    14小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信