Knockout validation for empty input

2.4k Views Asked by At

I've got problem with getting knockout validation to work as I want to. Let say we have ViewModel:

var viewModel = function ViewModel() {
    var self = this;

    self.number = ko.observable("").extend({ pattern: new RegExp("^(\\d{10})$") });

    self.submitFunction = function () {
        if (self.number.isValid()) {
            //SUBMIT
        }
    };
}

In this situation when self.number is empty function return true. It's great when we are adding error messages( field is not red from the start). But when I'm submitting value it should be considered invalid and UI validation suppose to be updated. What is good practice here?

0

There are 0 best solutions below