From 0359381937c4fc838df02548507b8af8d743737d Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Sat, 20 Jun 2020 12:55:40 -0400 Subject: [PATCH 1/9] fix(questionnaire): added phone number requirement using a regex to require users to input a valid phone number and stripped non numbers from the stored value with the exception of the country code --- app/assets/javascripts/validate.js | 12 ++++++++++++ app/models/questionnaire.rb | 6 ++++++ app/views/questionnaires/_form.html.haml | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index 6eec34797..687711a7d 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -40,6 +40,18 @@ document.addEventListener('turbolinks:load', function() { } } break; + case 'phone': + if (!value || $.trim(value).length < 1) { + notify(this, 'Missing Information'); + } + else if(value){ + var re = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; + if (!re.test(value)) { + notify(this, 'Please enter a valid phone number'); + success = false; + } + } + break; case 'file-max-size': if ( this.files && diff --git a/app/models/questionnaire.rb b/app/models/questionnaire.rb index 0f692613a..74d9558ab 100644 --- a/app/models/questionnaire.rb +++ b/app/models/questionnaire.rb @@ -8,6 +8,7 @@ class Questionnaire < ApplicationRecord before_validation :clean_for_non_rsvp before_validation :clean_negative_special_needs before_validation :clean_negative_dietary_restrictions + before_save :strip_phone_number after_create :queue_triggered_email_create after_update :queue_triggered_email_update after_save :update_school_questionnaire_count @@ -137,6 +138,11 @@ def vcs_url=(value) super value end + def strip_phone_number + # strips the string to just numbers and possible a plus sign for country code for standardization + self.phone = phone.tr('^0-9+', '') + end + def school School.find(school_id) if school_id end diff --git a/app/views/questionnaires/_form.html.haml b/app/views/questionnaires/_form.html.haml index fefc59e30..9fdd64dce 100644 --- a/app/views/questionnaires/_form.html.haml +++ b/app/views/questionnaires/_form.html.haml @@ -12,7 +12,7 @@ .form-inputs = f.input :first_name, input_html: { "data-validate" => "presence" }, autofocus: true, wrapper_html: { class: 'input--half' } = f.input :last_name, input_html: { "data-validate" => "presence" }, wrapper_html: { class: 'input--half' } - = f.input :phone, label: "Phone number", input_html: { "data-validate" => "presence" } + = f.input :phone, label: "Phone number", input_html: { "data-validate" => "phone" } = f.input :date_of_birth, start_year: Date.today.year - 5, end_year: Date.today.year - 90, order: [:month, :day, :year], input_html: { "data-validate" => "presence" } = f.input :school_id, as: :school_selection, input_html: { "data-validate" => "presence" } From 0bd4f3862ee9c586e87d5cec70b84a1245f38124 Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Tue, 14 Jul 2020 13:14:48 -0400 Subject: [PATCH 2/9] fix(questionnaire): edited callback to pass test edit call back that removes non-numbers from phone number to pass testing --- app/models/questionnaire.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/questionnaire.rb b/app/models/questionnaire.rb index 74d9558ab..8d7675650 100644 --- a/app/models/questionnaire.rb +++ b/app/models/questionnaire.rb @@ -8,7 +8,6 @@ class Questionnaire < ApplicationRecord before_validation :clean_for_non_rsvp before_validation :clean_negative_special_needs before_validation :clean_negative_dietary_restrictions - before_save :strip_phone_number after_create :queue_triggered_email_create after_update :queue_triggered_email_update after_save :update_school_questionnaire_count @@ -138,9 +137,10 @@ def vcs_url=(value) super value end - def strip_phone_number + def phone=(value) # strips the string to just numbers and possible a plus sign for country code for standardization - self.phone = phone.tr('^0-9+', '') + value = value.try(:tr, '^0-9+', '') + super value end def school From aa6d2c743e402efed9814053802260a2b8d39cc6 Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Fri, 17 Jul 2020 11:19:24 -0400 Subject: [PATCH 3/9] fix(questionnaire): edited test to fail with phone formatting edited test to fail if the phone # formating is still there when saved into the database. Also stopped exception for "+" as it was not necessary. --- app/models/questionnaire.rb | 2 +- test/controllers/manage/questionnaires_controller_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/questionnaire.rb b/app/models/questionnaire.rb index 8d7675650..ae0d5528c 100644 --- a/app/models/questionnaire.rb +++ b/app/models/questionnaire.rb @@ -139,7 +139,7 @@ def vcs_url=(value) def phone=(value) # strips the string to just numbers and possible a plus sign for country code for standardization - value = value.try(:tr, '^0-9+', '') + value = value.try(:tr, '^0-9', '') super value end diff --git a/test/controllers/manage/questionnaires_controller_test.rb b/test/controllers/manage/questionnaires_controller_test.rb index 8499dc1f0..88361c434 100644 --- a/test/controllers/manage/questionnaires_controller_test.rb +++ b/test/controllers/manage/questionnaires_controller_test.rb @@ -355,7 +355,7 @@ class Manage::QuestionnairesControllerTest < ActionController::TestCase assert_equal @user.id, @questionnaire.checked_in_by_id assert_equal true, @questionnaire.agreement_accepted assert_equal true, @questionnaire.can_share_info - assert_equal "(123) 333-3333", @questionnaire.phone + assert_equal "1233333333", @questionnaire.phone assert_equal "new_email@example.com", @questionnaire.email assert_match /Checked in/, flash[:notice] assert_response :redirect From 14625f26b64472e751a6409b278f34040d14c0fc Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Fri, 17 Jul 2020 23:10:08 -0400 Subject: [PATCH 4/9] fix(questionnaire): fixed comment about phone number stripping --- app/models/questionnaire.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/questionnaire.rb b/app/models/questionnaire.rb index ae0d5528c..cea00948e 100644 --- a/app/models/questionnaire.rb +++ b/app/models/questionnaire.rb @@ -138,7 +138,7 @@ def vcs_url=(value) end def phone=(value) - # strips the string to just numbers and possible a plus sign for country code for standardization + # strips the string to just numbers for standardization value = value.try(:tr, '^0-9', '') super value end From d19a22d1739cd6708f54be27af652be05ab1344b Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Fri, 17 Jul 2020 23:33:13 -0400 Subject: [PATCH 5/9] fix: houndci formatting comments --- app/assets/javascripts/validate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index 687711a7d..c23d6e655 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -45,7 +45,7 @@ document.addEventListener('turbolinks:load', function() { notify(this, 'Missing Information'); } else if(value){ - var re = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; + re = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; if (!re.test(value)) { notify(this, 'Please enter a valid phone number'); success = false; From 9f5968d46361145038fdcf31d4dfe1ce473811fe Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Fri, 17 Jul 2020 23:35:31 -0400 Subject: [PATCH 6/9] fix: earlier fix was incorrect. correct houndci fix --- app/assets/javascripts/validate.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index c23d6e655..7a7800a51 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -45,8 +45,8 @@ document.addEventListener('turbolinks:load', function() { notify(this, 'Missing Information'); } else if(value){ - re = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; - if (!re.test(value)) { + var phoneReg = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; + if (!phoneReg.test(value)) { notify(this, 'Please enter a valid phone number'); success = false; } From b0f54d3e598b7d6124124daed3c68583fdbeb4da Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Sat, 25 Jul 2020 03:19:14 -0400 Subject: [PATCH 7/9] fix(phone): implmented changed phone number regex for validation implement suggested regex although made internation extention 3 instead of 2 digits long for max length --- app/assets/javascripts/validate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index 7a7800a51..dbb47766a 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -45,7 +45,7 @@ document.addEventListener('turbolinks:load', function() { notify(this, 'Missing Information'); } else if(value){ - var phoneReg = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; + var phoneReg = /^[\+]?[0-9]{0,3}[-\s\.]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; if (!phoneReg.test(value)) { notify(this, 'Please enter a valid phone number'); success = false; From aab4955cae0c344924c3073a97f63c5b6a8bb6d9 Mon Sep 17 00:00:00 2001 From: Jeremy Rudman Date: Sat, 1 Aug 2020 16:17:09 -0400 Subject: [PATCH 8/9] fix(questionnaire): added support for more international numbers made the numbers regex more flexable to support more international numbers such as in Nigeria with two digit area codes. --- app/assets/javascripts/validate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index dbb47766a..efd6dba83 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -45,7 +45,7 @@ document.addEventListener('turbolinks:load', function() { notify(this, 'Missing Information'); } else if(value){ - var phoneReg = /^[\+]?[0-9]{0,3}[-\s\.]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/; + var phoneReg = /^[\+]?[0-9]{0,3}[-\s\.]?[(]?[0-9]{1,3}[)]?[-\s\.]?[0-9]{1,3}[-\s\.]?[0-9]{4,6}$/; if (!phoneReg.test(value)) { notify(this, 'Please enter a valid phone number'); success = false; From bf499493329cfbdafbfa4eec83c4e44c34848d47 Mon Sep 17 00:00:00 2001 From: Peter Kos Date: Sun, 20 Sep 2020 03:36:36 -0400 Subject: [PATCH 9/9] Phone validation hit, remove duplicate check --- app/assets/javascripts/validate.js | 5 +---- app/views/questionnaires/_form.html.haml | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index efd6dba83..8da91b2f8 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -41,10 +41,7 @@ document.addEventListener('turbolinks:load', function() { } break; case 'phone': - if (!value || $.trim(value).length < 1) { - notify(this, 'Missing Information'); - } - else if(value){ + if (value) { var phoneReg = /^[\+]?[0-9]{0,3}[-\s\.]?[(]?[0-9]{1,3}[)]?[-\s\.]?[0-9]{1,3}[-\s\.]?[0-9]{4,6}$/; if (!phoneReg.test(value)) { notify(this, 'Please enter a valid phone number'); diff --git a/app/views/questionnaires/_form.html.haml b/app/views/questionnaires/_form.html.haml index 4e8c45f8c..b895acebe 100644 --- a/app/views/questionnaires/_form.html.haml +++ b/app/views/questionnaires/_form.html.haml @@ -10,7 +10,7 @@ = markdown(HackathonConfig['disclaimer_message']) .form-inputs - = f.input :phone, label: "Phone number", input_html: { "data-validate" => "presence" } + = f.input :phone, label: "Phone number", input_html: { "data-validate" => ["presence", "phone"] } = f.input :date_of_birth, start_year: Date.today.year - 5, end_year: Date.today.year - 90, order: [:month, :day, :year], input_html: { "data-validate" => "presence" } = f.input :school_id, as: :school_selection, input_html: { "data-validate" => "presence" }