1

Here is some problems with it. I would like to ask you how to cope with this problem? Actually I'm new to Javascript. Thank you in advance.

It looks like this on the console:

Javascript_ Error in user.js TypeError: Cannot read property 'value' of undefined

function checkPassword(form) {
  var password = checkPassword(password);
  var s_letters = "qwertyuiopasdfghjklzxcvbnm"; 
  var b_letters = "QWERTYUIOPLKJHGFDSAZXCVBNM"; 
  var digits = "0123456789"; 
  var specials = "!@#$%^&*()_-+=\|/.,:;[]{}"; 
  var is_s = false; 
  var is_b = false;
  var is_d = false; 
  var is_sp = false; 

  for (var i = 0; i < password.length; i++) {
    if (!is_s && s_letters.indexOf(password[i]) != -1) is_s = true;
    else if (!is_b && b_letters.indexOf(password[i]) != -1) is_b = true;
    else if (!is_d && digits.indexOf(password[i]) != -1) is_d = true;
    else if (!is_sp && specials.indexOf(password[i]) != -1) is_sp = true;
  }

  var rating = 0;
  var text = "";
  if (is_s) rating++; 
  if (is_b) rating++; // 
  if (is_d) rating++; // 
  if (is_sp) rating++; // 
  if (password.length < 6 && rating < 3) text = "Bad";
  else if (password.length < 6 && rating >= 3) text = "Good";
  else if (password.length >= 8 && rating < 3) text = "Good";
  else if (password.length >= 8 && rating >= 3) text = "Excellent";
  else if (password.length >= 6 && rating == 1) text = "Bad";
  else if (password.length >= 6 && rating > 1 && rating < 4) text = "Good";
  else if (password.length >= 6 && rating == 4) text = "Excellent";
  console.log(text); // Alert will not work for us here. Console.log allows you to export data to the console for troubleshooting.
  return text;
}

var player = GetPlayer(); // Gets the player object.
var myPassword = player.GetVar("SystemDate"); // Gets the value for myPassword
console.log(myPassword);
var newValue = checkPassword(myPassword); // Run the function and return to newValue
player.SetVar("SystemDate",newValue); // Set the value of newValue back to Storyline
4

1 回答 1

0

我稍微改变了你的功能,试试那个(并向我们展示SetValue功能):

// @first error - password input, not the form
function checkPassword (password) {
    var expr, 
        rating = 0, 
        rules = {
            // Lower case
            '/[a-z]+/g': false,
            // Upper case
            '/[A-Z]+/g': false,
            // Numbers
            '/[0-9]+/g': false,
            // Special symbols
            '/[^\w\s]/gi': false
        };

    for (expr in rules) {
        if (password.match(expr)) {
            // rules[expr] = true; // - can be used to show the proper message of invalidation
            rating++;
        }
    }

    return 
        || password.length >= 8 && rating >= 3 && "Excellent"
        || password.length < 6 && rating >= 3 && "Good"
        || password.length >= 8 && rating < 3 && "Good"
        || password.length >= 6 && rating > 1 && rating < 4 && "Good"
        || "Bad";
}

// Gets the player object.
var player = GetPlayer(); 
// Gets the value for myPassword
var myPassword = player.GetVar("SystemDate"); 
// Run the function and return to newValue
var newValue = checkPassword(myPassword); 
// Set the value of newValue back to Storyline
// @second error seems to be in the function SetVar
player.SetVar("SystemDate",newValue); 
于 2017-06-19T11:43:10.483 回答