javascript - Regex validation for "no spaces" -
i have tried:
self.username = ko.observable(data.username || null) .extend({ required: true, maxlength: 50 }) .extend({ pattern: { message: 'username cannot contain spaces.', params: '^\s*$' } });
and
self.username = ko.observable(data.username || null) .extend({ required: true, maxlength: 50 }) .extend({ pattern: { message: 'username cannot contain spaces.', params: '[\s]' } });
but validation error flagged value of "a".
the expression '^\s*$'
seems work on on-line javascript regex testers. not sure why flagging.
when using string define regular expression, need escape backslashes, should be:
self.username = ko.observable(data.username || null) .extend({ required: true, maxlength: 50 }) .extend({ pattern: { message: 'username cannot contain spaces.', params: '^\\s*$' } });
if use regular expression literal instead of string, don't need escape backslash:
self.username = ko.observable(data.username || null) .extend({ required: true, maxlength: 50 }) .extend({ pattern: { message: 'username cannot contain spaces.', params: /^\s*$/ } });
Comments
Post a Comment