簡體   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