Bug 22862: Normalize SMS messaging numbers before validating them
authorKyle M Hall <kyle@bywatersolutions.com>
Tue, 7 May 2019 18:53:29 +0000 (14:53 -0400)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Mon, 24 Jun 2019 14:01:36 +0000 (15:01 +0100)
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 <wizzyrea@gmail.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

koha-tmpl/intranet-tmpl/prog/js/members.js
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt

index bcca8ef..ea1f7d7 100644 (file)
@@ -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);
index 9b211ce..9686306 100644 (file)
     });
     $("#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);
+});
+
 //]]>
 </script>
 [% END %]