0

This is my jquery to get data,

var passwordReset = {
        UserName: $("#txtLgnUsername").val(),
        Password: $("#hdnOldPassword").val(),
        NewPassword: $("#txtLgnPasswordReset").val()
    }

I don't want person to enter his userName as password, so I used this in next line

if (passwordReset.NewPassword.contains(passwordReset.UserName)) {
        alert(passwordReset.NewPassword);
        notifyMessage.showNotifyMessage('error', 'User name cannot be part of password.Please try a different password.', false);
    }

Which isn't helping...How do i check if my password has userName?? Thanks in advance...

Uzair Khan
  • 2,812
  • 7
  • 30
  • 48
  • @Vikrant [this is Javascript, not java](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java). It is however, a duplicate of [How can I check if one string contains another substring?](How can I check if one string contains another substring?) – Huey Apr 30 '15 at 10:30

2 Answers2

1
if (passwordReset.NewPassword.toLowerCase().indexOf(passwordReset.UserName.toLowerCase()) > -1) {
        alert(passwordReset.NewPassword);
        notifyMessage.showNotifyMessage('error', 'User name cannot be part of password.Please try a different password.', false);
    }
SirDemon
  • 1,758
  • 15
  • 24
1

Change your if condition to

if(passwordReset.NewPassword.indexOf(passwordReset.UserName)>-1){..}

indexOf will return -1 if username is not present in password. indexOf actually returns the position of the substring in the actual string and if not present, it returns -1.

Zee
  • 8,420
  • 5
  • 36
  • 58