@@ -, +, @@ --- Koha/Validation.pm | 138 ++++++++++++++++ .../bug_14620_email_and_phone_validations.sql | 2 + installer/data/mysql/sysprefs.sql | 2 + .../prog/en/modules/admin/preferences/patrons.pref | 16 ++ .../prog/en/modules/members/memberentrygen.tt | 67 ++++++++ .../bootstrap/en/modules/opac-memberentry.tt | 49 ++++++ .../bootstrap/en/modules/opac-messaging.tt | 59 +++++++ .../bootstrap/lib/bootstrap/css/bootstrap.css | 24 +++ .../bootstrap/lib/bootstrap/css/bootstrap.min.css | 2 +- members/memberentry.pl | 37 +++-- opac/opac-memberentry.pl | 25 ++- opac/opac-messaging.pl | 13 +- t/db_dependent/Validations.t | 184 +++++++++++++++++++++ 13 files changed, 595 insertions(+), 23 deletions(-) create mode 100644 Koha/Validation.pm create mode 100644 installer/data/mysql/atomicupdate/bug_14620_email_and_phone_validations.sql create mode 100644 t/db_dependent/Validations.t --- a/Koha/Validation.pm +++ a/Koha/Validation.pm @@ -0,0 +1,138 @@ +package Koha::Validation; + +# Copyright 2015 Vaara-kirjastot +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use strict; +use warnings; + +use C4::Context; +use Email::Valid; + +use vars qw($VERSION); + +=head1 NAME + +Koha::Validation - validates inputs + +=head1 SYNOPSIS + + use Koha::Validation + +=head1 DESCRIPTION + +This module lets you validate given inputs. + +=head2 validate_email + +Validates given email. + + Koha::Validation::validate_email("email@address.com"); + +returns: 1 if the given email is valid, 0 otherwise. + +=cut + +sub validate_email { + + my $address = shift; + + # make sure we are allowed to validate emails + return 1 if not C4::Context->preference("ValidateEmailAddress"); + return 1 if not $address; + return 0 if $address =~ /(^(\s))|((\s)$)/; + return (not defined Email::Valid->address($address)) ? 0:1; +} + +=head2 validate_phonenumber + +Validates given phone number. + + Koha::Validation::validate_phonenumber(123456789); + +returns: 1 if the given phone number is valid, 0 otherwise. + +=cut + +sub validate_phonenumber { + my $phonenumber = shift; + + # make sure we are allowed to validate phone numbers + return 1 if C4::Context->preference("ValidatePhoneNumber") eq "OFF"; + return 1 if not $phonenumber; + return 0 if $phonenumber =~ /(^(\s))|((\s)$)/; + + my $regex = get_phonenumber_regex(C4::Context->preference("ValidatePhoneNumber")); + return ($phonenumber !~ /$regex/) ? 0:1; +} + +=head2 get_phonenumber_regex + +Returns the used regex (according to ValidatePhoneNumber system preference) for phone numbers. +This is used to share the same regex between Perl scripts and JavaScript in templates. + + Koha::Validation::get_phonenumber_regex(); + +International phone numbers (ipn): http://regexlib.com/REDetails.aspx?regexp_id=3009 + +returns: the regex + +=cut + +sub get_phonenumber_regex { + if (C4::Context->preference("ValidatePhoneNumber") eq "ipn") { + 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,19}\d$/; + } + + return ""; + } + + +=head2 use_validator + +Validates given input with given validator. + + Koha::Validation::use_validator("phone", 123456789); + Koha::Validation::use_validator("email", "email@address.com"); + +Currently supported validators are + email, e-mail + phone, phonenumber + +returns: 1 if the given input is valid with the given validator, 0 otherwise. + +=cut + +sub use_validator { + my ($validator, $input) = @_; + + if (not defined $validator or not defined $input) { + warn "Subroutine must be called with validator and input"; + return 0; + } + + return validate_email($input) if $validator eq "email" or $validator eq "e-mail"; + return validate_phonenumber($input) if $validator eq "phone" or $validator eq "phonenumber"; + + warn "Validator not found"; + return 0; +} + +1; --- a/installer/data/mysql/atomicupdate/bug_14620_email_and_phone_validations.sql +++ a/installer/data/mysql/atomicupdate/bug_14620_email_and_phone_validations.sql @@ -0,0 +1,2 @@ +INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES ('ValidatePhoneNumber','OFF','ipn|fin|new|OFF','Validation of phone number on patrons.','Choice'); +INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) VALUES ('ValidateEmailAddress','0','','Validation of email address on patrons.','YesNo'); --- a/installer/data/mysql/sysprefs.sql +++ a/installer/data/mysql/sysprefs.sql @@ -477,6 +477,8 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('UseQueryParser','0',NULL,'If enabled, try to use QueryParser for queries.','YesNo'), ('UseTransportCostMatrix','0','','Use Transport Cost Matrix when filling holds','YesNo'), ('UseWYSIWYGinSystemPreferences','0','','Show WYSIWYG editor when editing certain HTML system preferences.','YesNo'), +('ValidateEmailAddress','0','','Validation of email address on patrons.','YesNo'), +('ValidatePhoneNumber','OFF','ipn|fin|new|OFF','Validation of phone number on patrons.','Choice'), ('viewISBD','1','','Allow display of ISBD view of bibiographic records','YesNo'), ('viewLabeledMARC','0','','Allow display of labeled MARC view of bibiographic records','YesNo'), ('viewMARC','1','','Allow display of MARC view of bibiographic records','YesNo'), --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -138,6 +138,22 @@ Patrons: yes: Allow no: "Don't allow" - librarians to discharge borrowers and borrowers to request a discharge. + - + - Use validator + - pref: ValidatePhoneNumber + choices: + ipn: International Phone Numbers + fin: Finnish Phone Numbers + new: ...Not yet implemented... + "OFF": none + - to validate phone numbers on patrons. + - + - pref: ValidateEmailAddress + choices: + yes: Enable + no: Disable + - validation of email addresses on patrons. + "Norwegian patron database": - - pref: NorwegianPatronDBEnable --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt @@ -34,8 +34,39 @@ } [% END %] $("#dateofbirth").datepicker({ maxDate: "-1D" }); + + var MSG_INCORRECT_PHONE = _("Please enter a valid phone number."); + $.validator.addMethod('phone', function(value) { + value = value.trim(); + if (!value.trim()) { + return 1; + } + else { + return (value.match(/[% phone_regex %]/)); + } + }, + MSG_INCORRECT_PHONE); + $("#entryform").validate({ rules: { + [% IF ( ValidatePhoneNumber ) %] + phone: { + phone: true + }, + phonepro: { + phone: true + }, + mobile: { + phone: true + }, + SMSnumber: { + phone: true + }, + B_phone: { + phone: true + }[% IF ( ValidateEmailAddress ) %],[% END %] + [% END %] + [% IF ( ValidateEmailAddress ) %] email: { email: true }, @@ -45,6 +76,7 @@ B_email: { email: true } + [% END %] }, submitHandler: function(form) { $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting'); @@ -52,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(); @@ -254,6 +309,18 @@ [% IF ERROR_bad_email_alternative %]