From 18114de9b749d96b065a177d5897ca756a035aae Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 7 May 2019 14:53:29 -0400 Subject: [PATCH] Bug 22862: Normalize SMS messaging numbers before validating them Librarians often copy and paste patron data, including phone numbers. SMS phone numbers are now being validated to conform to the E.164 specification. It would be nice to try to normalize that data by stripping non-numeric data from the paste (i.e. dashes, parens, etc ). Test Plan: 1) Apply this patch 2) On the staff side, Attempt to enter invalid characters the SMS number field 3) Note you cannot enter invalid characters 4) Attempt to paste a phone number with invalid characters 5) Note those characters are removed on paste 6) Repeat these steps on the OPAC Signed-off-by: Liz Rea --- koha-tmpl/intranet-tmpl/prog/js/members.js | 6 ++++++ .../bootstrap/en/modules/opac-messaging.tt | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/koha-tmpl/intranet-tmpl/prog/js/members.js b/koha-tmpl/intranet-tmpl/prog/js/members.js index bcca8ef83c..ea1f7d7707 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/members.js +++ b/koha-tmpl/intranet-tmpl/prog/js/members.js @@ -315,6 +315,12 @@ $(document).ready(function(){ function(value, element, phone) { var e164 = "^\\+?[1-9]\\d{1,14}$"; var re = new RegExp(e164); + + let has_plus = value.charAt(0) === '+'; + value = value.replace(/\D/g,''); + if ( has_plus ) value = '+' + value; + element.value = value; + return this.optional(element) || re.test(value); }, jQuery.validator.messages.phone); diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt index 9f4e6f7908..145a515447 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt @@ -185,6 +185,28 @@ }); $("#info_digests").tooltip(); }); + +function normalizeSMS(value){ + let has_plus = value.charAt(0) === '+'; + let new_value = value.replace(/[^0-9]+/g, ''); + if ( has_plus ) new_value = '+' + new_value; + return new_value; +} + +var sms_input = document.getElementById('SMSnumber'); + +sms_input.addEventListener('keyup', function(){ + var field = sms_input.value; + sms_input.value = normalizeSMS(field); +}); + +sms_input.addEventListener('paste', function(event) { + let paste = (event.clipboardData || window.clipboardData).getData('text'); + setTimeout(function () { + sms_input.value = normalizeSMS(paste); + }, 100); +}); + //]]> [% END %] -- 2.11.0