How would you modify this regex to only allow letters and numbers?
I thought by putting that it must have at least 2 uppercase letters, lowercase letters and digits, that it would limit the expression to just those types of characters. But it allows unwanted characters like tildes and such.
/^[\s]*((?=([^\s]*\d){2,})(?=([^\s]*[a-z]){2,})(?=([^\s]*[A-Z]){2,})[^\s]{8,16})[\s]*$/
Here's a way to do it using a few needles. fiddle
alert( verify('AbcdeFghij123') );
alert( verify('Abcdeghij123') ); // Only 1 capital
alert( verify('AbcdeFghij') ); // No numbers
alert( verify('ABCDEF123') ); // No lowercase
alert( verify('Abc~~ghij123') ); // Tilde
alert( verify('') ); // Blank
function verify(pass) {
return /^[A-Za-z0-9]+$/.test(pass)
&& /[A-Z][^A-Z]*[A-Z]/.test(pass)
&& /[a-z][^a-z]*[a-z]/.test(pass)
&& /[0-9][^0-9]*[0-9]/.test(pass);
}
If you want to limit the size to being between X and Y replace /^[A-Za-z0-9]+$/
with /^[A-Za-z0-9]{X,Y}$/
. Pretty simple, eh?