bootbox meteor form: preventing blank submissions

293 views Asked by At

I have a "create new" button linked to a bootbox modal form that asks the user to input the name of a new chat room. The code looks like this:

Template.chatsList.events = {
'click .newchat-button': function () {
bootbox.prompt("Enter a title for your new chat", function(result) {                
  if (result != null) {
    var validEntry = result;
    Rooms.insert({
      name: validEntry
    });
  }
});
}
}

However, on a blank submit, the submission still goes through and the room name is blank. How can I stop this from happening? I tried result.length > 1, etc, to no avail.

I'm a beginner and would really appreciate some explanation on the matter.

2

There are 2 answers

0
Michel Floyd On

bootbox.prompt() will execute the callback regardless of what the input is, even if blank. A common pattern instead is to use a custom bootbox.dialog() and disable the submit button until something has been input. You'd still allow cancel of course.

0
Firdaus Ramlan On

How about

if(result && result.length){
  ..
}

On blank submission, result value is empty string, not null.