-3

I am validating a text input. When someone enters something in my text input, I first of all remove the spaces from the input and then check for the existence of any character other than A-Z and a-z. When i validate the input using /^[A-Za-z]+$/.test(input_value), i get my desired result however /[a-z]/gi is not working properly. Is there something i am doing wrong here?

Here is what i want:

abCde //accepted

ABCde //accepted

abcDe3 // not accepted

aSc#-er // not accepted

ABCDE_ // not accepted

ABE_Ed // not accepted

  • The second example matches if there is a single alphabetic character (case-insensitive) in the input. Your first only matches if every character in the input is alphabetic. – Phylogenesis Sep 04 '20 at 15:39
  • What do i have to do to make the second one work in the same way as the first one – Haider imam Sep 04 '20 at 15:40

2 Answers2

0

Your two examples are quite different.

The first example (/^[A-Za-z]+$/) matches only when all characters are in the set [A-Za-z].

The second example (/[a-z]/gi) matches if only a single character is alphabetic.

I suspect you want /^[a-z]+$/i:

/
^      # Matches the start of the string
[a-z]+ # Matches one or more lower case letters
$      # Matches the end of the string
/i     # Case insensitive matching
Phylogenesis
  • 7,775
  • 19
  • 27
-1

Probably the problem is that you do not use the specials chars ^, + and $