password validation in jquery using regular expression – regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase.
pattern
var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
password validation in jquery using regular expression
We use jQuery Validate plugin to validate user-inputs. It all works except the password-validation: The strong password should meet some minimum requirements:
- at least 8 characters
- at least 1 number
- at least 1 lowercase character (a-z)
- at least 1 uppercase character (A-Z)
- only 0-9a-zA-Z
Regular Expression for Password:
Example
function checkPassword(password){ var pattern = /^[a-zA-Z0-9_-]{5,15}$/; if(pattern.test(password)){ return true; }else{ return false; } }
Result
wellCome = Ok well@Come = Error
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/
Regular Expression for Strong Password:
var pattern = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%&]).*$/;
Result
wellCome@9898 = Ok well@Come = Error
REGULAR EXPRESSION WITH JQUERY VALIDATION
Regular Expression for Email:
function used to check valid email
//function used to check valid email function checkEmail(email) { //regular expression for email var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); if(pattern.test(email)){ return true; } else { return false; } }
Result
pakainfo.info.com = Error [email protected] = Ok
Regular Expression for URL:
Example
function checkUrl(url) { //regular expression for URL var pattern = /^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-z]{2,4}/; if(pattern.test(url)){ return true; } else { return false; } }
Result
www.pakainfo.com = Error http://pakainfo.com = Ok http://www.pakainfo.com = Ok
Regular Expression for Username:
Example
function checkUsername(username){ var pattern = /^[a-z0-9_-]{4,15}$/; if(pattern.test(username)){ return true; }else{ return false; } }
Result
pakainfo.name = Error adminnew123 = Ok
Email and password validation in jQuery
I hope you get an idea about password validation in jquery using regular expression.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.