From 3adc3dd9f0d07fa83be2874e57cdea7d037c637c Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Thu, 13 Aug 2015 15:25:06 +0300 Subject: [PATCH] Bug 14620 - Whitespace fix for validations This fix trims emails and phone numbers just before form is sent. Fixes the invalid syntax used in JQuery's .on() functions' argument. Removes the trimming for click events as change event should be enough. Test plan: 1. Apply the patches (and run updatedatabase.pl). 2. Set system preferences ValidateEmailAddress to "Enable" and ValidatePhoneNumber to "International Phone Numbers". 3. Make sure JavaScript is enabled. 4. Navigate to edit user contact informations in Staff client and OPAC. 5. Clear email and phone fields. 6. Insert whitespaces to email and phone fields and submit the form. 7. Edit patron. 8. Observe that there are no longer whitespaces in email and phone fields. 9. Insert invalid email (e.g. "invalid") and invalid phone number ("+123invalid") and send the form. 10. Confirm that form will not be submitted and errors will be given. 11. Disable JavaScript. 12. Insert invalid email and invalid phone number 13. Try sending the form again. 14. Confirm that form will be submitted but errors will be given on top of the form. 15. Observe that none of the changes are accepted. 16. Set system preferences ValidateEmailAddress to "Disable" and ValidatePhoneNumber to "none". 17. Repeat step 3-9 but observe that errors will not be given and changes will be submitted and saved. --- Koha/Validation.pm | 6 ++---- .../prog/en/modules/members/memberentrygen.tt | 23 ++++++++++++++++++++++ .../bootstrap/en/modules/opac-memberentry.tt | 16 +++++++++++++++ .../bootstrap/en/modules/opac-messaging.tt | 11 +++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/Koha/Validation.pm b/Koha/Validation.pm index fb3bc3d..61f1b6c 100644 --- a/Koha/Validation.pm +++ b/Koha/Validation.pm @@ -53,6 +53,7 @@ sub validate_email { # make sure we are allowed to validate emails return 1 if not C4::Context->preference("ValidateEmailAddress"); + return 0 if $address =~ /(^(\s))|((\s)$)/; return 0 if not defined $address; return (not defined Email::Valid->address($address)) ? 0:1; } @@ -70,15 +71,12 @@ returns: 1 if the given phone number is valid, 0 otherwise. sub validate_phonenumber { my $phonenumber = shift; - $phonenumber =~ s/\s+//g; - # make sure we are allowed to validate phone numbers return 1 if C4::Context->preference("ValidatePhoneNumber") eq "OFF"; return 1 if $phonenumber eq ""; return 0 if not defined $phonenumber; my $regex = get_phonenumber_regex(C4::Context->preference("ValidatePhoneNumber")); - #die $regex; return ($phonenumber !~ /$regex/) ? 0:1; } @@ -100,7 +98,7 @@ sub get_phonenumber_regex { return qr/^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}$/; } elsif (C4::Context->preference("ValidatePhoneNumber") eq "fin") { - return qr/^((90[0-9]{3})?0|\+358\s?)(?!(100|20(0|2(0|[2-3])|9[8-9])|300|600|700|708|75(00[0-3]|(1|2)\d{2}|30[0-2]|32[0-2]|75[0-2]|98[0-2])))(4|50|10[1-9]|20(1|2(1|[4-9])|[3-9])|29|30[1-9]|71|73|75(00[3-9]|30[3-9]|32[3-9]|53[3-9]|83[3-9])|2|3|5|6|8|9|1[3-9])\s?(\d\s?){4,20}$/; + return qr/^((90[0-9]{3})?0|\+358\s?)(?!(100|20(0|2(0|[2-3])|9[8-9])|300|600|700|708|75(00[0-3]|(1|2)\d{2}|30[0-2]|32[0-2]|75[0-2]|98[0-2])))(4|50|10[1-9]|20(1|2(1|[4-9])|[3-9])|29|30[1-9]|71|73|75(00[3-9]|30[3-9]|32[3-9]|53[3-9]|83[3-9])|2|3|5|6|8|9|1[3-9])\s?(\d\s?){4,19}\d$/; } return ""; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt index b8be701..203f9d1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt @@ -84,10 +84,33 @@ return false; else form.beenSubmitted = true; + [% IF ValidateEmailAddress %] + $("#email, #emailpro, #B_email").each(function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] + [% IF ValidatePhoneNumber %] + $("#phone, #phonepro, #B_phone, #SMSnumber").each(function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] form.submit(); } }); + [% IF ValidateEmailAddress %] + $("#email, #emailpro, #B_email").on("change", function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] + [% IF ValidatePhoneNumber %] + $("#SMSnumber").on("change", function(){ + $(this).val($.trim($(this).val())); + }); + $("#phone, #phonepro, #B_phone").on("change", function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] var mrform = $("#manual_restriction_form"); var mrlink = $("#add_manual_restriction"); mrform.hide(); diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt index d08f9d2..f637c4e 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt @@ -870,6 +870,16 @@ } else { form.beenSubmitted = true; + [% IF ValidateEmailAddress %] + $("#borrower_email, #borrower_emailpro, #borrower_B_email").each(function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] + [% IF ValidatePhoneNumber %] + $("#borrower_phone, #borrower_phonepro, #borrower_B_phone, #SMSnumber").each(function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] form.submit(); } }, @@ -883,6 +893,12 @@ error.css('width', 'auto'); } }); + $("#borrower_email, #borrower_emailpro, #borrower_B_email").on("change", function(){ + $(this).val($.trim($(this).val())); + }); + $("#borrower_phone, #borrower_phonepro, #borrower_B_phone").on("change", function(){ + $(this).val($.trim($(this).val())); + }); }); //]]> 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 dc3edf5..b700659 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt @@ -179,6 +179,11 @@ } else { form.beenSubmitted = true; + [% IF ValidatePhoneNumber %] + $("#SMSnumber").each(function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] form.submit(); } }, @@ -193,6 +198,12 @@ } }); + [% IF ( ValidatePhoneNumber ) %] + $("#SMSnumber").on("change", function(){ + $(this).val($.trim($(this).val())); + }); + [% END %] + }); //]]> -- 1.9.1