Search code examples
javascriptvalidationknockout.jsknockout-validation

Button works when the required fields are not filled after you fill and delete what is written


I have a page where if first name and last name is not filled addAddress button will not work. This piece of code works perfectly if you press addAddress button it asks for those required fields to be filled. When you will it, it works, but if you just fill it and the delete what you have written, button will work again. I do not get the idea what is the problem?

    self.addAddress = function () {
    if (self.FirstName() != undefined && self.LastName() != undefined) {
        self.selectedAddress(new Address(true));
    }

    else {
        self.errors.showAllMessages();

    }
};

Solution

  • I think when you delete it it is no longer undefined but empty string, so checking empty string should do the trick

    self.addAddress = function () {
    if (self.FirstName() !== '' && self.LastName() !== '') {
        self.selectedAddress(new Address(true));
    }
    
    else {
        self.errors.showAllMessages();
    
    }