0

I am using basic input component for validating name.

It is accepting only characters and not allowing any special characters to enter. This condition is working fine.

Validating code:

      firstname: [null, [Validators.required, Validators.pattern('[a-zA-Z][a-zA-Z]+')]],

But i need one more requirement:
1) It should accept even a single character, but it is accepting minimum 2 character by default.How can i change this default behaviour??

Here is the stackblitz link.

2 Answers 2

2

Problem is with the regular expression you have written in Validators.pattern(), it looks for 2 characters minimum, remove on [a-zA-z] block and it should work fine as you desire.

Sign up to request clarification or add additional context in comments.

3 Comments

Ya i resolved this, but i want space between the characters and should avoid space at the beginning of the 1st character.
For ex It should accept: 1)arun kumar but it should not accept 2) arun kumar.
try this regular expression [a-zA-Z]+([a-zA-Z ]+)* should work.
1

Try reduce regex pattern to [a-zA-Z]+ It will take from 1 to unlimited amount of characters. Yor current regex takes char then unlimited amount of characters so it need minimal two characters to match.

3 Comments

It's working fine, but space at thing it shouldn't take. How can i solve this?
Try ^[a-zA-Z]+?[ ][a-zA-Z]+$ It will accept unlimited characters and additional space with unimited characters
This is allowing space between words and not allowing at the beginning of the first word but my 1st issue which i mentioned in the question exists again if i use this expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.