   'submit form.set-username': (event, template) ->
     event.preventDefault()
 
-    usernameSubmitted = true
+    usernameFormMessages.resetMessages()
     username = $(template.findAll 'input.username').val()
+
+    usernameFormMessages.setErrorMessage 'Username is required.', 'username' unless username
+
     try
-      usernameFormMessages.resetMessages()
+      User.validateUsername username, 'username'
+    catch error
+      usernameFormMessages.setError error
+
+    if usernameFormMessages.isValid()
+      usernameSubmitted = true
       Meteor.call 'set-username', username, (error) ->
         return usernameFormMessages.setError error if error
         resetUsernameForm()
-        usernameFormMessages.setInfoMessage "Username set successfully"
-    catch error
-      usernameFormMessages.setError error
+        usernameFormMessages.setInfoMessage "Username set successfully."
 
     return # Make sure CoffeeScript does not return anything
 

  else
    $('input.current-password').val('')
    $('input.new-password').val('')
  newPasswordReadyForValidation = false
  newPasswordFieldModified = false



    if passwordFormMessages.isValid()
      changePassword currentPassword, newPassword, (error) ->
        return passwordFormMessages.setError error if error
        resetPasswordForm template
        passwordFormMessages.setInfoMessage "Password changed successfully."

    return # Make sure CoffeeScript does not return anything

  'keyup input.current-password, paste input.current-password': (event, template) ->
    # Proceed only if something really changed
    newValue = $(template.findAll 'input.current-password').val()
    return if newValue is currentPasswordLastValue
    currentPasswordLastValue = newValue

    passwordFormMessages.resetMessages '' # Reset global messages

    # We cannot verify validity of the password on the client, so
    # let's at least remove any error message to not confuse the user
    # with a message that password is still invalid. In other fields
    # we are providing feedback immediately, so we have to keep this
    # the same. We cannot say that field is invalid if in fact maybe
    # it is not. Better to err on the side of validity than saying that
    # something is wrong when it is valid.
    passwordFormMessages.resetMessages 'current-password'

    return # Make sure CoffeeScript does not return anything

