Friday, July 4

Regex for Validation

What is Regex?

Regex (or regular expression) is sequence of special characters that  match or find string by using pattern.
(Pattern is sequence characters in regex). Which can be use to search, validate or edit text and data.



Regex for Validation

Validate username with regular expression

Condition : Match characters and symbols in the list, a-z, 0-9, underscore, hyphen Length at least 3 characters and maximum length of 15

Regex : ^[a-z0-9_-]{3,15}$

Code Example

Validate email with regular expression

Condition : Simple it should be an email address

Regex :  ^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

Code Example

Validate password with regular expression

Condition :
 must contains one digit from 0-9
 must contains one lowercase characters
 must contains one uppercase characters
 contains one special symbols in the list "@#$%"
 match anything with previous condition checking
 length at least 6 characters and maximum of 20

Regex : ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})
Code Example

No comments:

Post a Comment

Ads Inside Post