diff --git a/app/assets/javascripts/validate.js b/app/assets/javascripts/validate.js index 6eec34797..8da91b2f8 100644 --- a/app/assets/javascripts/validate.js +++ b/app/assets/javascripts/validate.js @@ -40,6 +40,15 @@ document.addEventListener('turbolinks:load', function() { } } break; + case 'phone': + 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'); + success = false; + } + } + break; case 'file-max-size': if ( this.files && diff --git a/app/models/questionnaire.rb b/app/models/questionnaire.rb index 2a2436ac2..1cb47671e 100644 --- a/app/models/questionnaire.rb +++ b/app/models/questionnaire.rb @@ -138,6 +138,12 @@ def vcs_url=(value) super value end + def phone=(value) + # strips the string to just numbers for standardization + value = value.try(:tr, '^0-9', '') + super value + 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 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" } diff --git a/test/controllers/manage/questionnaires_controller_test.rb b/test/controllers/manage/questionnaires_controller_test.rb index 03a992781..ab7f84023 100644 --- a/test/controllers/manage/questionnaires_controller_test.rb +++ b/test/controllers/manage/questionnaires_controller_test.rb @@ -390,7 +390,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