Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to not allow String Start or End With Special Character and should not have consecutive special characters

I want to set a password in my application. Password should not start and end with special characters and special characters can be added between the string. It may have special characters, But it should not have consecutive special characters . Here i have the regular expression that should not allow user to not enter string's starting character and ending character with special characters .

var string='877sswere';
var a= RegExp(/^[a-zA-Z0-9](.*[a-zA-Z0-9])?$/).test(string);
console.log(a);

so my requirement is

string='877sswere' -> Correct
string='@877sswere' -> In Correct
string='!877sswere^' -> In Correct
string='877sswere$' -> In Correct
string='8&*77sswere' -> In Correct
string='87#7ssw$er^e' -> Correct
string='877^sswere' -> Correct

So can any one suggest me to how to do this? Any help is greatly appreciated. Thanks in advance.

like image 636
jeeva Avatar asked Jul 31 '14 15:07

jeeva


1 Answers

This regex should work:

/^[a-z0-9](?!.*?[^\na-z0-9]{2}).*?[a-z0-9]$/gmi

RegEx Demo

like image 70
anubhava Avatar answered Sep 18 '22 07:09

anubhava