1

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
Ante Ereš
  • 623
  • 4
  • 8
  • 24
  • 1
    It's unclear, your pattern is actually allowing spaces in many places. Could you give some example inputs that should match and some that should not match ? – Pac0 May 03 '18 at 10:56
  • also strongly related : [regular-expression-to-match-non-english-characters](https://stackoverflow.com/questions/150033/regular-expression-to-match-non-english-characters/873600#873600) – Pac0 May 03 '18 at 10:58
  • I edited my question – Ante Ereš May 03 '18 at 11:02
  • wait...so you want to invalidate single spaces but allow multiple spaces or the other way around? – Liam May 03 '18 at 11:09
  • the sentence *only one **or more spaces** (??) I must notice him.. But if he continue inserting some other charachters it's ok.* contradicts itself. – Liam May 03 '18 at 11:10
  • If pattern matches with only space characters it's invalid. And if there are some other characters beside spaces, pattern is valid. Then space entry is allowed also – Ante Ereš May 03 '18 at 11:13
  • 1
    It seems you just want to require at least one non-space char, use `Validators.pattern("\\s*\\S.*")` or `Validators.pattern(/^\s*\S.*$/)` – Wiktor Stribiżew May 03 '18 at 11:17
  • That's what I was looking for. Thank you Wiktor. I don't understand this regex so much – Ante Ereš May 03 '18 at 11:20

1 Answers1

2

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563