diff --git a/app/views/shared/_accessible_input.html.erb b/app/views/shared/_accessible_input.html.erb index f0fe0d8..8c42dc3 100644 --- a/app/views/shared/_accessible_input.html.erb +++ b/app/views/shared/_accessible_input.html.erb @@ -1,8 +1,13 @@ -<% tip = "#{id}_tip" %> -<% error = "#{id}_error" %> +<% +tip = "#{id}_tip" +error = "#{id}_error" -<% tooltip = tooltip ||= '' %> -<% error_handler = error_handler ||= '' %> +# Used to prevent JS bindings from affecting existing page elements +js_id = SecureRandom.hex + +tooltip = tooltip ||= '' +error_handler = error_handler ||= '' +%>
@@ -11,62 +16,71 @@ name="<%= name %>" class="left-indent <%= classes ||= '' %>" value="<%= val ||= '' %>" - aria-describedby="<%= tooltip ||= '' %>" /> + aria-describedby="<%= tooltip ||= '' %>" + roadmap-js-id="<%= js_id %>" /> - <% if type == 'password' %> + <% + # If the field is a password then provide a 'show password' checkbox + if type == 'password' + %>
- +
<% end %> - <% if !tooltip.blank? %> -
+ <% + # If the field has a tooltip add it and wire it up for on focus/blur + if !tooltip.blank? + %> +
<%= tooltip %>
<% end %> - <% # If an error handler was passed in or the input type has a generic handler %> - <% if !error_handler.blank? || ['email', 'password'].include?(type) %> - + <% + # If an error handler was passed in or the input type has a generic handler + if !error_handler.blank? || ['email', 'password'].include?(type) + %> + diff --git a/app/views/shared/_register_form.html.erb b/app/views/shared/_register_form.html.erb index 96ffa9a..d1f2c5a 100644 --- a/app/views/shared/_register_form.html.erb +++ b/app/views/shared/_register_form.html.erb @@ -3,34 +3,34 @@
- <%= f.label :firstname, _('First Name') %> - <%= f.text_field :firstname, as: :firstname, class: 'input-small left-indent' %> + <%= render partial: 'shared/accessible_input', + locals: {type: 'text', id: 'user_firstname', name: 'user[firstname]', label: _("First Name"), + classes: 'required input-small'} %>
- <%= f.label :surname, _('Last Name') %> - <%= f.text_field :surname, as: :surname, class: 'input-small left-indent' %> + <%= render partial: 'shared/accessible_input', + locals: {type: 'text', id: 'user_surname', name: 'user[surname]', label: _("Last Name"), + classes: 'required input-small'} %>
- <%= f.label :email, _('Email') %> - <%= f.email_field :email, as: :email, class: 'input-medium left-indent' %> + <%= render partial: 'shared/accessible_input', + locals: {type: 'email', id: 'user_email', name: 'user[email]', label: _("Email"), + classes: 'required'} %> - <%= f.label :recovery_email, _('Recovery Email') %> - <%= f.email_field :recovery_email, as: :recovery_email, class: 'input-medium left-indent' %> + <%= render partial: 'shared/accessible_input', + locals: {type: 'email', id: 'user_recovery_email', name: 'user[recovery_email]', + label: _("Recovery Email"), classes: 'required'} %> - <%= f.label :password, _('Password') %> - <%= f.password_field :password, as: :password, class: 'input-medium left-indent' %> + <%= render partial: 'shared/accessible_input', + locals: {type: 'password', id: 'user_password', name: 'user[password]', label: _("Password"), + classes: 'required'} %>
- - - -
- <%= render partial: 'shared/accessible_submit_button', - locals: {id: 'create-account-button', + locals: {id: 'register-button', val: 'Create account', disabled_initially: true, classes: 'small-input-button', - tooltip: _('Enter your name, email and password.')} %> + tooltip: _('Enter all of the information above')} %>
<% end %> diff --git a/lib/assets/javascripts/roadmap-form.js b/lib/assets/javascripts/roadmap-form.js index 3409d9f..143a424 100644 --- a/lib/assets/javascripts/roadmap-form.js +++ b/lib/assets/javascripts/roadmap-form.js @@ -2,7 +2,6 @@ }); - // --------------------------------------------------------------------------- function validatePassword(sPassword) { if (sPassword.trim().length >= 8 && sPassword.trim().length <= 128){ diff --git a/lib/assets/javascripts/shared/register_form.js b/lib/assets/javascripts/shared/register_form.js index 4e197cb..563fe81 100644 --- a/lib/assets/javascripts/shared/register_form.js +++ b/lib/assets/javascripts/shared/register_form.js @@ -1,71 +1,37 @@ -$(document).ready(function() { - var email_regex = /[^@\s]+@(?:[-a-z0-9]+\.)+[a-z]{2,}$/; - var valid_email = false; - var valid_password = false; - var valid_password_confirmation = false; - var valid_accept_terms = false; +$(document).ready(function(){ + // If the hidden valid-form field is set to true then enable the submit button + $("form.user-registration #valid-form").change(function(){ + $(this).siblings(".form-submit").attr('aria-disabled', $(this).val() != "true"); + }); - $("#user_email.text_field.reg-input").change(function(){ - if (email_regex.test($(this).val())) { - $(this).next().hide(); - valid_email = true; - } - else { - $(this).next().show(); - valid_email = false; - } - set_aria_submit(); - }); - $("#user_password.text_field.reg-input").change(function() { - if($(this).val().length >= 8) { - $(this).next().hide(); - valid_password = true; - } - else { - $(this).next().show(); - valid_password = false; - } - // If password_confirmation is non empty already, force to check its validity - if($("#user_password_confirmation.text_field.reg-input").val().length > 0){ - console.log('force to check validity of confirmation'); - $("#user_password_confirmation.text_field.reg-input").change(); - } - set_aria_submit(); - }); - $("#user_password_confirmation.text_field.reg-input").change(function() { - if ($(this).val() === $("#user_password.text_field.reg-input").val()) { - $(this).next().hide(); - valid_password_confirmation = true; - } - else { - $(this).next().show(); - valid_password_confirmation = false; - } - set_aria_submit(); - }); - $("#user_accept_terms").change(function(){ - valid_accept_terms = $(this).prop('checked'); - set_aria_submit(); - }); - function set_aria_submit(){ - if(valid_email - && valid_password - && valid_password_confirmation - && valid_accept_terms){ - $("#sign_up_submit").attr('aria-disabled', false); - } - else { - $("#sign_up_submit").attr('aria-disabled', true); - } + $("form.user-registration input[class*='required']").change(function(){ + toggleRegisterSubmit(); + }); + + $("form.user-registration #user_email, form.user-registration #user_recovery_email").blur(function(){ + emailsMatch($(this).parent('form')); + }); + + function toggleRegisterSubmit(){ + let disabled = ($("#user_firstname").val().trim().length <= 0 || + $("#user_surname").val().trim().length <= 0 || + $("#user_email").val().trim().length <= 0 || + $("#user_recovery_email").val().trim().length <= 0 || + $("#user_password").val().trim().length <= 0 || + $("#user_email").val() === $("#user_recovery_email").val()); + $("form.user-registration #register-button").attr('aria-disabled', disabled); + } + + function emailsMatch(form){ + let email = $(this).find('#user_email'); + let recovery = $(this).find('#user_recovery_email'); + + if($(email).val().trim() === $(recovery).val().trim()){ + $("span." + $(email).attr('roadmap-js-id') + "_error").html('').attr('role', ''); + $("input[roadmap-js-id='" + $(email).attr('roadmap-js-id') + "']").removeClass('red-border'); + }else{ + $("span." + $(email).attr('roadmap-js-id') + "_error").attr('role', 'tooltip'); + $("input[roadmap-js-id='" + $(email).attr('roadmap-js-id') + "']").addClass('red-border'); } + } }); - -function toggleRegisterSubmit(){ - let disabled = ($("#user_firstname").val().trim().length <= 0 || - $("#user_surname").val().trim().length <= 0 || - $("#user_email").val().trim().length <= 0 || - $("#user_recovery_email").val().trim().length <= 0 || - $("#user_password").val().trim().length <= 0 || - $("#user_email").val() === $("#user_recovery_email").val()); - $("#register-button").attr('aria-disabled', disabled); -} \ No newline at end of file diff --git a/lib/assets/stylesheets/roadmap-form.scss b/lib/assets/stylesheets/roadmap-form.scss index 7264ddc..9dc7e01 100644 --- a/lib/assets/stylesheets/roadmap-form.scss +++ b/lib/assets/stylesheets/roadmap-form.scss @@ -200,7 +200,7 @@ .error-tooltip { display: none; - width: 397px; + width: 270px; } .error-tooltip[role='tooltip'] { @@ -314,6 +314,7 @@ } #new_user fieldset { input[type="password"], + #user_password, input[type="email"] { width: 395px; }