繁体   English   中英

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

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

这是我的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返回,所以isValidPassword功能甚至不火,如果有返回falseisValidEmail之前功能。 如何进行所有验证检查,然后向用户显示所有验证错误? 我假设我只应使用一个返回truefalse函数,然后再做一个函数,该函数将显示所有验证错误消息并返回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');
      }
    });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM