简体   繁体   中英

Regular expression still allowing tab character to be pasted into form field

Does anyone know why my regex below is still allowing a tab character whitespace to be pasted into a form field? I think I wrote it correctly...

/^[a-zA-Z\s]+$/g

Suggestion from answer to post:

self.City = ko.observable(model.City).extend({ required: true, maxLength: 30, pattern: /^[a-zA-Z ]+$/ });

\\s allows a tab and many more whitespace chars.

If you need a literal space replace \\s with a space.

You do not need the global modifier either.

Use

/^[a-zA-Z ]+$/

In Knockout, use

self.City = ko.observable(model.City)
    .extend({ required: true })
    .extend({ maxLength: 30 })
    .extend({ pattern: {
              message: 'Only letters and spaces are allowed.',
              params: '^[a-zA-Z ]+$'
     }});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM