1

Can anyone tell me how to print a message in installshield supported javascript instead of alert? I tried with the below code but alert is'nt working. And another problem is that I am unable to validate password by using RegExp.

    function Passwordvalidation()
    {
        var password= Session.Property("PASSWORD");
        var patt = new RegExp(":\\[A-Za-z0-9]{6,20}$\\","ig");
        var validpassword = patt.test(password);
        if(validpassword)
            {
                GetMD5(); //calls another function
                return true;
            }
        alert("password should have 6 to 20 characters which contains alphabets and digits between 0 to 9");
        return false;
    }
  • What do you mean by "I am unable to validate"? Use "^" instead of ":\\". Have a look [here](https://learn.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions). – Jeroen Heier Jan 13 '18 at 07:25
  • I tried with "^[A-Za-z0-9]{6,20}$" but it did not work. – jacksparrow Jan 13 '18 at 10:52
  • See [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/48346033#48346033) – ctwheels Jan 19 '18 at 18:23

3 Answers3

1

As @some suggested, you should first only check if the regex works.

But reading the error message "password should have 6 to 20 characters which contains alphabets and digits between 0 to 9" , I think that this part of the regex [A-Za-z0-9]{6,20}$ does not do what you think it does.

This matches any uppercase or lowercase character or digit repeated between 6 and 20 times at the end of the string. This would also match $$$$$444444 or aaaaaa

For example:

var patt = new RegExp('[a-z0-9]{6,20}$', "i");
var passwords = [
  "aaaaaa",
  "333333",
  "a12b3c",
  "AAAAAA",
  "$$$$$444444"
];

for (var i = 0; i < passwords.length; i++) {
  console.log(passwords[i] + " : " + patt.test(passwords[i]));
}

With ^:

var patt = new RegExp('^[a-z0-9]{6,20}$', "i");
var passwords = [
  "aaaaaa",
  "333333",
  "a12b3c",
  "AAAAAA",
  "$$$$$444444"
];

for (var i = 0; i < passwords.length; i++) {
  console.log(passwords[i] + " : " + patt.test(passwords[i]));
}

The modifier i makes it case insensitive, so you could update your regex to [a-z0-9]{6,20}$ or [A-Z0-9]{6,20}$ You can also omit the g modifier to prevent wrong results.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Yes [A-Za-z0-9]{6,20} works well as per the requirement.I have mistaken in alert message.So any other suggestions? – jacksparrow Jan 13 '18 at 13:00
  • The regexp is wrong. That is one of the problems. It should probably be `/^[A-Za-z0-9]{6,20}$/` – some Jan 13 '18 at 13:08
  • The OP commented "I tried with "^[A-Za-z0-9]{6,20}$" but it did not work.". It is not clear how exactly it was tried. – The fourth bird Jan 13 '18 at 13:22
  • /^[A-Za-z0-9]{6,20}$/ - Tried with this regex also. – jacksparrow Jan 16 '18 at 04:49
  • @Thefourthbird function PasswordValidation() { var Passwordportal = Session.Property("PASSWORDPORTAL"); var patt = /^[A-Za-z0-9]{6,20}$/; var passvalid = patt.test(Passwordportal); if (passvalid) { GetMd5(); Session.Property("PASSVALIDFLAG") = "0"; return true; } – jacksparrow Jan 16 '18 at 04:52
  • What does `var Passwordportal` contain? Can you check with `console.log(Passwordportal);console.log(typeof Passwordportal);`? – The fourth bird Jan 16 '18 at 07:00
0

It is probably because var patt = new RegExp(":\\[A-Za-z0-9]{6,20}$\\","ig"); throws an exception since that is an illegal regexp.

You don't need to use new RegExp but can define it as a literal regexp:

var patt = /^[A-Za-z0-9]{6,20}$/;

Since you defined ranges A-Z and a-z you don't need the i flag, and you don't need the g flag either.

some
  • 48,070
  • 14
  • 77
  • 93
0

This answer explains password restriction validation with regex really nicely:

Let's say that we want our password to:

  • Contain between 8 and 15 characters
  • Must contain an uppercase letter
  • Must contain a lowercase letter
  • Must contain a digit
  • Must contain one of special symbols

Then we can write a regex like this:

^(?=.{8,15}$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#$%^&*]).*$
 \__________/\_________/\_________/\_________/\______________/
    length      upper      lower      digit        symbol
Justinas Marozas
  • 2,482
  • 1
  • 17
  • 37