I have the following Javascript:
$(document).ready(function() {
if($('input[type=password]').getAttribute("id") == "password_form") {
$('input[type=password]').keyup(function() {
var pswd = $(this).val();
//validate length
if(pswd.length < 8) {
$('#length').removeClass('valid').addClass('invalid');
} else {
$('#length').removeClass('invalid').addClass('valid');
}
//validate letter
if ( pswd.match(/[A-z]/) ) {
$('#letter').removeClass('invalid').addClass('valid');
} else {
$('#letter').removeClass('valid').addClass('invalid');
}
//validate capital letter
if ( pswd.match(/[A-Z]/) ) {
$('#capital').removeClass('invalid').addClass('valid');
} else {
$('#capital').removeClass('valid').addClass('invalid');
}
//validate number
if ( pswd.match(/\d/) ) {
$('#number').removeClass('invalid').addClass('valid');
} else {
$('#number').removeClass('valid').addClass('invalid');
}
}).focus(function() {
$('#password_info').show();
}).blur(function() {
$('#password_info').hide();
});
} else {
var pswd = $(this).val();
var pswd2 = $(document).getElementById("password_form").val();
$('input[type=password]').keyup(function() {
if(pswd != pswd2) {
$('same').removeClass('valid').addClass('invalid');
} else {
$('same').removeClass('invalid').addClass('valid');
}
}).focus(function() {
$('#password_info_re-enter').show();
}).blur(function() {
$('#password_info_re-enter').hide();
});
}
});
I have two password fields since one is the first one you write your password in and the second one is for verification in a sign-up form. But I want to show password requirements on the first password box but a different box on the verification box showing whether the password matches or not.
So to do this I tried to see if I could find the ID of one of them so that the first part only applied to the first one and not the second one.
How would I do this?
$('input[type=password]').getAttribute("id") == "password_form"what is the question ?$('input[type=password]:eq(0)').getAttribute("id")isn't a jQuery method and will throw error ...useattr('id')or$('input[type=password]')[0].getAttribute("id")and keep in mind using id as the selector makes more sense as the generic one will only get value of first matching selector