I have a php page where I manage the password change request, I'm having trouble calling a javascript function to check if the new password has at least a special character and then calling again the same page to let the php update the password field of the database.
This is my html:
<form name="changePassword" onsubmit="return CheckPassword()" method="post"
accept-charset="utf-8">
Current Password:
<input name="currPwd" type="password" size="40" maxlength="200" id="currPwd"
placeholder="Current Password" required/>
New Password:
<input name="newPwd" type="password" size="40" maxlength="200" id="newPwd"
placeholder="New Password" required/>
Repeat New password:
<input name="rNewPwd" type="password" size="40" maxlength="200" id="rNewPwd"
placeholder="Repeat New Password" required/>
<input type="submit" id="changepsw" value="Change Password"/>
</form>
And this is my javascript code:
function CheckPassword () {
var pass1 = document.getElementById('newPwd');
var pass2 = document.getElementById('rNewPwd');
var isOk = true;
var res1 = /^(?=.*[ -\/:-@\[-_])([\w -\/]).+$/.test(pass1);
var res2 = /^(?=.*[ -\/:-@\[-_])([\w -\/]).+$/.test(pass2);
if ((res1 == false) || (res2 == false)) {
isOk = false;
} else {
$.ajax({
type: 'POST',
url: 'updatepswd.php',
data: $'form'.serialize()
});
}
return isOk;
}
Clicking on the submit button it should call the js function and that should check if the password does contain at least a special character and then call the php to update but what happens is that it calls the php page even if the password doesn't have any special characters.
Am I missing something or doing something wrong?
Edit:
I also did this without using jquery adding the .value to the password field when using it in the test function:
var res1 = /^(?=.*[ -\/:-@\[-_])([\w -\/]).+$/.test(pass1.value);
and I didn't use ajax but instead:
document.changePassword.submit();
$("#form").serialize()event.preventDefault()inside the block after or beforeisOk = false;