Ante Ereš
Ante Ereš

Reputation: 713

Javascript - Add validation pattern to allow at least one non-space char

I want to create input and add validation pattern to not allow spaces in input I found some solutions like this :

Validators.pattern(".*\\S.*[a-zA-z0-9 ]")

But problem with this pattern is that special charachters (č,ć,ž,đ,š...) are not included

So I need solution without blank (space) input but with special charachters

EDIT

For Example if someone insert only one or more spaces I must notice him.. But if he continue inserting some other charachters it's ok.

Example :

"   " - not valid

"   Ante Ereš" - valid

Upvotes: 1

Views: 2318

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

You may use

Validators.pattern("\\s*\\S.*")

to match a string that contains at least one non-whitespace character. Note that ^ and $ anchors are added automatically by Angular and the resulting pattern looks like /^\s*\S.*$/.

Pattern details

  • ^ - start of string
  • \s* - 0+ whitespace chars
  • \S - a non-whitespace
  • .* - any 0+ chars other than line break chars
  • $ - end of string.

Upvotes: 3

Related Questions