Handle exception “JavaScript runtime error: Unable to get property ‘length’ of undefined or null reference” on ko.observable field?

I’m using Knockoutjs on a registration form. I have runtime error when user click button to show password field

JavaScript runtime error: Unable to get property ‘length’ of undefined or null reference

how to handle runtime exception in KO.js?

   self.showHidePasswordField = function () {

        if (self.password1().length > 0 || typeof self.password1().length !== 'undefined') {
            console.log('show');
        } else {
            console.log('hide');
        }

    };

Answer

Assuming you are sure you defined the password1 observable, you can try the following:

if (self.password1() != null && typeof self.password1().length !== 'undefined')

Leave a Reply

Your email address will not be published. Required fields are marked *