How to allow only alphabets as a first character in input using angular 2 pipe

1.3k Views Asked by At

I used a concept of taking a length of the input field and restrict the numbers and symbols as a first character but it doesn't work out. Can some one help me by using pipe concept to allow only a alphabet as a first character.

Thanks in advance

1

There are 1 best solutions below

1
Srijan On

You can use regular expression, like:

^[a-zA-Z][a-zA-Z0-9.,$;]+$?
Explanation:

^ Start of line/string.
[a-zA-Z] Character is in a-z or A-Z.
[a-zA-Z0-9.,$;] Alphanumeric or . or , or $ or ;.
+ One or more of the previous token (change to * for zero or more).
$ End of line/string.

The special characters I have chosen are just an example. Add your own special characters as appropriate for your needs. Note that a few characters need escaping inside a character class otherwise they have a special meaning in the regular expression.

I am assuming that by "alphabet" you mean A-Z. Note that in some other countries there are also other characters that are considered letters.