Check if password has at least 1 capital letter

Ah, sorry, I was going all technical on myself then, when it was easier than I thought

Does this look ok to you, as it seems to be causing me problems.

else if (newpassword.length !> 5 && newpassword.length !< 51) {
document.getElementById('buttonSubmit').style.display = 'block';
swal({
title: 'Error!',
text: 'You password needs to be between 6 and 50 characters long',
type: 'error',
confirmButtonText: 'Return'
} )

You can add min and max values to the regex {6,50} if that’s easier.

1 Like

is it like this cpradio, as tried it with normal brackets too and it doesnt seem to be reading the 2 length values

var is_correct_format = /^(?=.*\d){6,50}(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/.test(newpassword);

No, that isn’t valid syntax.

No, that is likely to be a failed attempt too. I actually think adding it to the regex will be a lot harder.

// if newpassword length is less than 6 or greater than 50
else if (newpassword.length < 6 || newpassword.length > 50) {
document.getElementById('buttonSubmit').style.display = 'block';
swal({
title: 'Error!',
text: 'You password needs to be between 6 and 50 characters long',
type: 'error',
confirmButtonText: 'Return'
} )

Hi cpradio I have a little issue as I’m testing the change password option.

The first validation is to make sure they fill all the fields in, that works fine, then next then is that they use a capital and a number and this is where it doesnt seem to work corectly.

I type A1 as the new password and A1 to confirm password, and it doesnt validate, it still comes back as an error. I was hoping that A1 would validate but then it would go to the next validation to make sure its 6 or over and less than 50.

var oldpassword = $("input[name=\"old_password\"]").val();
var newpassword = $("input[name=\"new_password\"]").val();
var confirmpassword = $("input[name=\"confirm_password\"]").val();

var is_correct_format = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$/.test(newpassword);

if (newpassword === '' || confirmpassword === '' || oldpassword === ''){
document.getElementById('buttonSubmit').style.display = 'block';
swal({
title: 'Error!',
text: 'You need to fill in all the fields',
type: 'error',
confirmButtonText: 'Return'
})
} else if (!is_correct_format) {
document.getElementById('buttonSubmit').style.display = 'block';
swal({
title: 'Error!',
text: 'You need to use at least 1 capital and 1 number.',
type: 'error',
confirmButtonText: 'Return'
})
} else if (newpassword.length < 6 || newpassword.length > 50) {
document.getElementById('buttonSubmit').style.display = 'block';
swal({
title: 'Error!',
text: 'You password needs to be between 6 and 50 characters long',
type: 'error',
confirmButtonText: 'Return'
})
} else if (confirmpassword != newpassword){ 
document.getElementById('buttonSubmit').style.display = 'block';
swal({
title: 'Error!',
text: 'It seems like you have typed your confirm password incorrectly',
type: 'error',
confirmButtonText: 'Return'
} )
}

I got it, silly me testing with A1, as I need the undercase to not just the uppercase, sorry

When it comes to input validation, my worry is not so much about users with JS disabled (although that’s a legitimate concern) but that it’s easily bypassed by malicious users who may try to break the system.

3 Likes

Agreed, there is nothing wrong with having client-side validation, it’s a good idea, but is for UX purposes only, not security.
It should always be backed up by robust server-side validation to be (more) secure.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.