At least 2 letters in regex pattern

How can I execute at least 2 letters in the following regex?

/^[a-zA-Z0-9\_\-\.\+]+$/

For now, I am removing everything that is not a letter and counting the result with help strlen

to achieve what I need.

+3


source to share


2 answers


It works:

^[a-zA-Z0-9_.+-]*(?:[a-zA-Z][a-zA-Z0-9_.+-]*){2,}$



Expanded:

 ^ 
 [a-zA-Z0-9_.+-]* 
 (?:
      [a-zA-Z] [a-zA-Z0-9_.+-]* 
 ){2,}
 $

      

+2


source


You can use the following if the letters mean anything in the set you are using:

(/^[a-zA-Z0-9_-.+]{2,}$/     //here you can remove escape 
                             //characters inside character set

      

You can use the following if the letters mean [a-zA-Z]

:



/^(?=.*[a-zA-Z].*[a-zA-Z])[a-zA-Z0-9_-.+]+$/

      

Explanation:

  • (?=.*[a-zA-Z].*[a-zA-Z])

    look ahead with at least two letters
+1


source







All Articles