Please help Meteor isServer & isClient

I’m a fresh newbie into programming and website development and need some help.

In a registration form, I have two fields where the user is supposed to type in their password (and confirm this in the 2nd field absiously). Using Meteor’s isServer and isClient, I need to send some warning message (alert, pop-up, anything) from the server (isServer) to the user (isClient) whenever the passwords don’t match.

How can I do this writing JavaScript in Meteor?

Thanks so much in advance!

I think password matching validations should really be kept on the client… especially since the form can be bypassed anyway, if the user wanted to do that for whatever reason, then that’s up to them!

The way to do it however would be to use a Meteor method and throw an error when they don’t match, which the client will receive:

// server
Meteor.methods({
  createUser: function (password, passwordConfirm) {
    if (password !== passwordConfirm) {
      throw new Meteor.Error('password-mismatch', 'Passwords do not match');
    }
  }
})

// client
Meteor.call('createUser', password, passwordConfirm, function (error, result) {
  if (error.error === 'password-mismatch') {
    alert('Passwords do not match');
    // or
    alert(error.reason); // 'Passwords do not match'
  }
});

Thanks so much, @reoh!

Now, I need to kind of connect the code you wrote to the actual form. How do I do this?

The form-code is as follows:

Sounds to me like you need to read through the tutorial: https://www.meteor.com/tutorials/blaze/forms-and-events

1 Like

It’d look something like this:

Template.testing.events({
  'click #createUser': function (event, template) {
    var password = template.$('#password').val();
    var confirmPassword = template.$('#confirm_password').val();

    Meteor.call('createUser', password, confirmPassword, function (error) {
      // code
    });
  }
});

It worked perfectly, just had to do some editings. Thanks, @reoh :relaxed: