diff --git a/Src/knockout.validation.js b/Src/knockout.validation.js index ab1422c3..33e97d9b 100644 --- a/Src/knockout.validation.js +++ b/Src/knockout.validation.js @@ -617,7 +617,7 @@ validation.rules['phoneUS'] = { validator: function (phoneNumber, validate) { if (!validate) return true; - if (typeof (phoneNumber) !== 'string') { return false; } + if (phoneNumber !== null && typeof (phoneNumber) !== 'string') { return false; } if (utils.isEmptyVal(phoneNumber)) { return true; } // makes it optional, use 'required' rule if it should be required phoneNumber = phoneNumber.replace(/\s+/g, ""); return validate && phoneNumber.length > 9 && phoneNumber.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/); diff --git a/Tests/validation-tests.js b/Tests/validation-tests.js index 65896b6f..bf048507 100644 --- a/Tests/validation-tests.js +++ b/Tests/validation-tests.js @@ -572,6 +572,15 @@ test('Object is NOT Valid and isValid returns False', function () { equal(testObj.isValid(), false, 'testObj is not valid'); }); +test('Object is null and isValid returns True', function () { + var testObj = ko.observable('').extend({ phoneUS: true }); + + testObj(null); + + equal(testObj(), null, 'observable still works'); + equal(testObj.isValid(), true, 'testObj is valid'); +}); + //#endregion //#region Custom Rule Validation