Friday 9 June 2017

Validation alert in modal dialog

There is a user list in my Node-Express-Mongoose app. I need to click on the Update link to open a modal and update the user-info by clicking on the save changes button on the modal. Screenshot below:

Modal

Whenever i submit the form in the modal without any info, it doesn't alert me. below is the javascript for the same:

$(document)
  .on('click', '.update_user', function(e) { //click on update link
    e.preventDefault();
    $('#edit_user').unbind('click');
    $('#edit_user').click(function() { //click on save changes button


      var user_name = $("#input_name").val();
      var user_age = $("#input_age").val();

      if (user_name == '' && user_age == '') {
        alert("Please Fill All Required Fields");
        return false;
      }

      var user_data = {
        user_name: user_name,
        user_age: user_age
      };


      $.ajax({
        url: '/users/update_user',
        type: 'post',
        ContentType: 'application/json',
        data: JSON.stringify(user_data),
        success: function(data, textStatus, jQxhr) {
          console.log(data);
          load_users();
        },
        error: function(jqXhr, textStatus, errorThrown) {
          console.log(errorThrown);
        }
      });

    });

  });

Where is the issue?



via Arjun Jain

No comments:

Post a Comment