How to add custom message to template check in ko check

Let's say I have:

self.UserName = ko.observable("").extend({ required: true }).extend({ pattern: '[\S]' }); 

I am trying to do something like (I tried several options):

 self.UserName = ko.observable("").extend({ required: true }).extend({ pattern: '[\S]', message : 'cannot contain spaces' }); 

But no luck.

I see that check bindings mention check messages, but it seems to be for a single message for the whole check.

And the custom validation rules seem more murdered, since all I need is a template, but with a different message.

Am I missing something obvious?

+7
source share
1 answer

Your syntax is incorrect. You need to assign an object to the pattern property, which contains message and params

So the correct use is:

 self.UserName = ko.observable("") .extend({ required: true }) .extend({ pattern: { message: 'cannot contain spaces', params: '[\S]' }}); 

Se is also an example of getting started .

+11
source

All Articles