jquery - If validation condition is met, then run main chat code after clicking button -


how can make main chat code only run if validation passed (i.e. fields not contain default values or empty) after user clicks on sendmessage button?

right main chat code runs anyway whether validation passed or not.

// main chat code  $(function () {      var iwannachat = $.connection.mychathub;      iwannachat.client.addmessage = function (message) {         $('#listmessages').append('<li>' + message + '</li>');     };      $("#sendmessage").click(function () {         iwannachat.server.send($('#ideabody').val());     });      $.connection.hub.start(); });  // handling default values , validation  $(function () {     $('#ideatitle, #ideabody').each(function () {         $.data(this, 'default', this.value);     }).focus(function () {         if (!$.data(this, 'edited')) {             this.value = "";         }     }).change(function () {         $.data(this, 'edited', this.value != "");     }).blur(function () {         if (!$.data(this, 'edited')) {             this.value = $.data(this, 'default');         }     });     $('#sendmessage').click(function () {         $('#ideatitle,#ideabody').each(function () {             if ($.trim($(this).val()).length === 0) {                 alert('empty');                 return false;             }             if ($.trim($(this).val()) === $(this).data('default')) {                 alert('default');                 return false;             }         })     }); }); 

you can try use 1 $("#sendmessage").click

// main chat code  $(function () {  var iwannachat = $.connection.mychathub;  iwannachat.client.addmessage = function (message) {     $('#listmessages').append('<li>' + message + '</li>'); };  $("#sendmessage").click(function () {         if ($.trim($("#ideabody").val()).length === 0) {             alert('empty');             return false;         }         if ($.trim($("#ideabody").val()) === $("#ideabody").data('default')) {             alert('default');             return false;         }         else{             iwannachat.server.send($('#ideabody').val());         }         });  $.connection.hub.start(); });  // handling default values , validation  $(function () { $('#ideatitle, #ideabody').each(function () {     $.data(this, 'default', this.value); }).focus(function () {     if (!$.data(this, 'edited')) {         this.value = "";     } }).change(function () {     $.data(this, 'edited', this.value != ""); }).blur(function () {     if (!$.data(this, 'edited')) {         this.value = $.data(this, 'default');     } }); }); 

Comments

Popular posts from this blog

java - Last square of 8x8 grid not drawn, using two nested FOR loops -

What design pattern is this code in Javascript? -

javascript - Unusual behaviour when drawing lots of images onto a large canvas -