I’m playing with a password validator script, and I’m doing fine with length, letters, caps, numbers and spaces. But I’m not sure how to detect special characters.
Here’s my jquery script:
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$('#password1').keyup(function() {
var pswd = $(this).val();
//validate the length
if(pswd.length < 6 ){ $('#length').removeClass('valid').addClass('invalid'); }
//validate letter
if(pswd.match(/[A-z]/)){ $('#letter').removeClass('invalid').addClass('valid'); }
//validate capital letter
if(pswd.match(/[A-Z]/)){ $('#capital').removeClass('invalid').addClass('valid'); }
//validate number
if(pswd.match(/\d/)){ $('#number').removeClass('invalid').addClass('valid'); }
//validate no spaces
if(pswd.match(/\s/)){ $('#spaces').removeClass('valid').addClass('invalid'); }
//validate symbols
if(pswd.match(/\D/)){ $('#symbol').removeClass('invalid').addClass('valid'); }
});
});
</script>
I admit I got this offline, from somewhere I cannot recall. As I was testing the script, I noticed the Symbols were always validated whenever a letter was typed.
Is if(pswd.match(/\D/)){ wrong?
\Djust means “not a digit”, so any letter will work. If you want “not alphanumeric”, you want[^a-zA-Z0-9]. Side-note, your[A-z]should be[A-Za-z]or the whole thing should be/[a-z]/i