From: Kyle M Hall Date: Tue, 7 May 2019 18:53:29 +0000 (-0400) Subject: Bug 22862: Normalize SMS messaging numbers before validating them X-Git-Url: http://git.equinoxoli.org/?p=koha-equinox.git;a=commitdiff_plain;h=115f4f2045a6c695d1eed47cd401b7759630ac4d 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 Signed-off-by: Katrin Fischer Signed-off-by: Martin Renvoize --- diff --git a/koha-tmpl/intranet-tmpl/prog/js/members.js b/koha-tmpl/intranet-tmpl/prog/js/members.js index bcca8ef..ea1f7d7 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 9b211ce..9686306 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 %]