流星 - 自定义注册.显示所有验证错误,而不是一个

Meteor - custom signup. Show all validation errors instead of one

本文关键字:一个 错误 注册 自定义 显示 验证 流星      更新时间:2023-09-26

这是我的JS(流星)代码:

  Template.register.events({
    'submit #register-form': function(event, template) {
      event.preventDefault();
      //Reset sessions
      Session.set("PwFieldErr", false);
      Session.set("EmFieldErr", false);
      // Get input values
      var email = template.find('#account-email').value,
          password = template.find('#account-password').value,
          repeatPassword = template.find('#confirm-password').value;
      // Check if inputs not empty
      // Trim Email
      var trimInput = function(val) {
        return val.replace(/^'s*|'s*$/g, "");
      }
      var email = trimInput(email);
      // Validate Email
      var emailRe = new RegExp("^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(?:'.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@(?:[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?'.)*(?:aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$");
      var isValidEmail = function(val) {
        if (emailRe.test(val)) {
          return true;
        } else {
          sAlert.error('Invalid email!');
          Session.set("EmFieldErr", true);
          return false;
        }
      }
      // Validate Password
      var re = /^(?=.*'d)(?=.*[a-zA-Z]).{6,}$/;
      var isValidPassword = function(val, rval) {
        if (re.test(val) && val == rval) {
          return true;
        } else if (!re.test(val)) {
          sAlert.error('Your password must be at least 6 characters long and contain at least 1 number');
          Session.set("PwFieldErr", true);
          return false;
        } else if (rval != val) {
          sAlert.error('Your passwords does not match');
          Session.set("PwFieldErr", true);
          return false;
        }
      }
      // If Password ok -> Register user
      if (isValidEmail(email) && isValidPassword(password, repeatPassword)) {
        Accounts.createUser({
          email: email,
          password: password
        }, function(error) {
          if (error) {
            // Inform the user that account creation failed
            sAlert.error(error.reason);
          } else {
            // Success. Account has been created and the user
            // has logged in successfully.
            sAlert.success('Account created successfully');
          }
        });
      }
      return false;
    }
  });

目前,此代码在返回第一个false后停止,因此如果之前有从isValidEmail函数返回false isValidPassword函数甚至不会触发。如何进行所有验证检查,然后向用户显示所有验证错误?我假设我应该只使用一个返回true & false,然后我应该再做一个函数,它将显示所有验证错误消息并返回truefalse

只需将调用弹出到它们自己的变量中:

  var validEmail = isValidEmail( email );
  var validPW = isValidPassword( password, repeatPassword );
  if (validEmail && validPw) {
    Accounts.createUser({
      email: email,
      password: password
    }, function(error) {
      if (error) {
        // Inform the user that account creation failed
        sAlert.error(error.reason);
      } else {
        // Success. Account has been created and the user
        // has logged in successfully.
        sAlert.success('Account created successfully');
      }
    });
  }