Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a regular expression of minimum 8, maximum 16, alphabetic, numbers and NO space?

Tags:

regex

I am trying to check text with a regular expression in iOS, and below is my code. My regular expression is accepting one word or number which it should be minimum 8 and maximum 16 with numbers or alphabetic.

if (![self validate:txtPass.text regex:@"^[a-zA-Z0-9]+$"]){

   NSLOG(@"Check value");
}

What should I change in my regular expression?

like image 323
Luai Kalkatawi Avatar asked Nov 15 '13 14:11

Luai Kalkatawi


3 Answers

^[a-zA-Z0-9]{8,16}$

You can specify the minimum/maximum gathered using {X,Y} as the boundaries.

Other examples:
^[a-zA-Z0-9]{8,}$ #8 or more characters
^[a-zA-Z0-9]{,16}$ #Less than or equal to 16 characters
^[a-zA-Z0-9]{8}$ #Exactly 8 characters

Regex cheatsheet

like image 166
Trenton Trama Avatar answered Nov 01 '22 18:11

Trenton Trama


You just need to put bounds on, not the + (one or more) operator.

^[a-zA-Z0-9]{8,16}$
like image 23
C.B. Avatar answered Nov 01 '22 19:11

C.B.


^[A-Za-z0-9]{8,16}$

Specify a min/max length for a part of an expression using {x,y}, where x is the minimum and y is the maximum.

like image 28
qJake Avatar answered Nov 01 '22 19:11

qJake