如何使用json数组进行密码验证?

时间:2018-04-05 20:42:58

标签: javascript jquery arrays passwords

我正在尝试循环json数组以进行密码验证。有人可以帮助我。

int main(int argc, char** argv)
{
  bool continueProcessing = true;
  std::vector<std::string> args;
  for(int i = 1; i < argc; i++)
  {
    args.push_back(std::string(argv[i]));
  }

  if(std::find(args.begin(), args.end(), "-h") != args.end())
  {
    /* Help entry goes here*/
    continueProcessing = false;
  }
  if(std::find(args.begin(), args.end(), "-o") != args.end())
  {
    outputFile = const_cast<char*>((*++std::find(args.begin(),args.end(), "-o")).c_str())
  }
}

3 个答案:

答案 0 :(得分:0)

您可以使用这段代码使用密码

测试每个表达式
var valid = true;
var message;

for(var k in pwdarray.pwd){
    var regex = new RegExp(pwdarray.pwd[k].EXPRESSION);
    if(pswd.search(regex) > -1){
        valid = false;
        message = pwdarray.pwd[k].TEXT;
        break;
    }
}

答案 1 :(得分:0)

首先,您需要访问EXPRESSION pwdarray['pwd'][index]['EXPRESSION'],其中index是整数。这可能来自for-loop。其次,您需要使用正则表达式match() pswd

var pwdarray = { 
  "pwd": [ 
    {  
     "TEXT":"Password must be at least 6 character(s) long.",
     "EXPRESSION":"\/(^(.){6,}$)\/"
    },
    {  
     "TEXT":"Password must contain at least 1 lowercase letter(s).",
     "EXPRESSION":"/[a-z]/"
    }
  ]
};

var pswd = "test";

for (var index in pwdarray['pwd'])
  if (pswd.match (pwdarray['pwd'][index]['EXPRESSION']))
    $('#length').removeClass('invalid').addClass('valid');
  else {
    $('#length').removeClass('valid').addClass('invalid');
    document.write (pwdarray['pwd'][index]['TEXT']);
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 2 :(得分:0)

不要将/放在表达式周围。然后,您可以将其转换为new RegExp的正则表达式,并使用它测试密码。您可以使用.every()循环,只要他们成功就会执行测试。

&#13;
&#13;
var pwdarray = {
  "pwd": [{
      "TEXT": "Password must be at least 6 character(s) long.",
      "EXPRESSION": "^(.){6,}$"
    },
    {
      "TEXT": "Password must contain at least 1 lowercase letter(s).",
      "EXPRESSION": "[a-z]"
    }
  ]
}
$("#check").click(function() {
  var pswd = $("#pass").val();
  $("#error").text("Password is good").addClass('valid').removeClass('invalid');
  pwdarray.pwd.every(({
    TEXT: text,
    EXPRESSION: exp
  }) => {
    if (!new RegExp(exp).test(pswd)) {
      $("#error").text(text).addClass('invalid').removeClass('valid');
      return false;
    } else {
      return true;
    }
  });
});
&#13;
.invalid {
  color: red;
}

.valid {
  color: green;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="pass">
<button id="check">Check</button>
<div id="error"></div>
&#13;
&#13;
&#13;