Custom Error message for minLength Knockout Validations
Asked Answered
B

2

5

I am working with knockout validations . I want to display custom message for min and max length .I have tried with some options and I cant able to achieve it.if anyone achieved it already, please share me how I can do that.

here is what I am trying right now.

var viewModel = {
    firstName: ko.observable().extend({
        minLength: [
            3,
             'Please enter Valid number']

        , maxLength: 10
    }),
}

Do I have to really go for the RegEx for this.

Balfour answered 24/2, 2014 at 10:28 Comment(0)
E
10

In you want to provide a custom message you need pass in an object to the validation rule (minLength) with a params property holding the parameter and a message property with the new message:

var viewModel = {
    firstName: ko.observable().extend({
        minLength: { params: 3, message: "Please enter Valid number" }
        , maxLength: 10
    }),
}

Demo JSFiddle.

Elliellicott answered 24/2, 2014 at 10:33 Comment(1)
yeah.. I have passed the parameter as param missed (s).Thanks for making it up.Balfour
F
1

To customize Native-Rules you need to pass literal object with properties that you want to customize:

For Example:

var viewModel = {
    firstName: ko.observable().extend({
        minLength: {
            params:3,
            message:'Please enter Valid number'
         }
        , maxLength: 10
    }),
}

To get more understanding kindly check this SO answer here

Fries answered 24/2, 2014 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.