Question

I need a login which only checks the first letter of a password and ignores anything behind of it. Here is the standard login-code I got from my last script. I know Its very not secure for web solutions:

var unArray = ["ExampleTom"];   //User
var pwArray = ["7******];"]     //Password
for (var i=0; i <unArray.length; i++) {
if ((un == unArray[i]) && (pw == pwArray[i])) {
    valid = true;

The idea is that the user should type any number as he wants as a password, but if it starts with a 7 he succeeds (The '7***' is just written to illustrate my idea).

Was it helpful?

Solution

I don't know why would you ever do that but here it is:

var unArray = ["ExampleTom"];   //User
var pwArray = ["7******];"]     //Password

valid = false

for (var i=0; i <unArray.length; i++) {
  if ((pwArray[i]. charAt(0) === pw.charAt(0)) && (un == unArray[i])) {
     valid = true;
  }
}

OTHER TIPS

code below should help you is I correctly undersood your idea:

var unArray = ["ExampleTom"];   //User
var pwArray = ["7******];"]     //Password
if ((un == unArray) && (pw[0] == pwArray[0])) {
    valid = true;
}

If you have the password input string, you can simply check if the first character is seven, like this:

var pw = "74534SomePassword";
pw[0] == 7; // true

var pw = "foo74534SomePassword";
pw[0] == 7; // false

Since for some reason, your password appears to be stored in an array, you'll have to use:

pwArray[i][0]

to check if the string in ["7******];"] really starts with a seven.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top