0

I have a web api application in which I have this form :

     <form id="editForm">

                    <input id="editid" type="hidden" name="id" />
                    <p><label>Numéro cnss </label><input id="cnss" name="cnss" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Nom </label><input id="lastname" name="lastname" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Prénom </label><input id="firstname" name="firstname" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Adresse   </label> @Html.TextArea("address", new { id = "address" })<label style="color:red" id="labError"> ( * )</label></p>

                    <p><label>Lieu de naissance </label><input id="birth_place" name="birth_place" /> </p>
                    <p><label>Mutuelle </label><input id="mutuelle" name="mutuelle" /><label style="color:red" id="labError"> ( * )</label></p>
                    <p><label>Caisse d'assurance maladie </label><input id="caisse_assu_maladie" name="caisse_assu_maladie" /><label style="color:red" id="labError"> ( * )</label></p>

                    <p>
                        <label>Type de client   </label>
                        <select id="client_type" name="client_type">
                            <option value="demande de transport régulière sur une période">demande de transport régulière sur une période</option>
                            <option value="demande de transport régulière et indéfinie">demande de transport régulière et indéfinie</option>
                            <option value="Ponctuel ">Ponctuel </option>
                        </select>
                    </p>

                </form>
 <button id="btnStepTwo" type="submit" class="btn btn-primary" >Commencer la réservation</button>

in the Api controller, I have this action :

 [HttpPost]
        public bool ValidateClient(ClientModel item)
        {
            return ModelState.IsValid;  
        }

I'd like to add a javascript method for every change in the form, this method calls the ValidateClient service to verify if the model in the form is valid or not : If the model is valid ==> the button will be enabled, otherwise it will be disabled.

  1. How can I accomplish this task?
  2. What is the best and easiest way?
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191

1 Answers1

1

You attach a change() event to the inputs:

$('input').on('change', function (e) {
    var el = $(e.currentTarget);
    $.post(url, $('form').serialize(), function (data) {
        if (data) {
            // do stuff to "el"
        }
    });
});

From a REST perspective, you may want to respond with a proper HTTP code. Not sure a 200 response makes sense if the model is invalid.

beautifulcoder
  • 10,832
  • 3
  • 19
  • 29