View | Details | Raw Unified | Return to bug 14620
Collapse All | Expand All

(-)a/Koha/Validation.pm (+80 lines)
Line 0 Link Here
1
package Koha::Validation;
2
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 2 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use C4::Context;
23
use Email::Valid;
24
25
=head1 NAME
26
27
Koha::Validation - validates inputs
28
29
=head1 SYNOPSIS
30
31
  use Koha::Validation
32
33
=head1 DESCRIPTION
34
35
This module lets you validate given inputs.
36
37
=head2 METHODS
38
39
=head3 email
40
41
Koha::Validation::email("email@address.com");
42
43
Validates given email.
44
45
returns: 1 if the given email is valid (or empty), 0 otherwise.
46
47
=cut
48
49
sub email {
50
    my $address = shift;
51
52
    return 1 unless $address;
53
    return 0 if $address =~ /(^(\s))|((\s)$)/;
54
55
    return (not defined Email::Valid->address($address)) ? 0:1;
56
}
57
58
=head3 phone
59
60
Koha::Validation::validate_phonenumber(123456789);
61
62
Validates given phone number.
63
64
returns: 1 if the given phone number is valid (or empty), 0 otherwise.
65
66
=cut
67
68
sub phone {
69
    my $phonenumber = shift;
70
71
    return 1 unless $phonenumber;
72
    return 0 if $phonenumber =~ /(^(\s))|((\s)$)/;
73
74
    my $regex = C4::Context->preference("ValidatePhoneNumber");
75
    $regex = qr/$regex/;
76
77
    return ($phonenumber !~ /$regex/) ? 0:1;
78
}
79
80
1;
(-)a/installer/data/mysql/atomicupdate/Bug_14620-Contact-information-validation.perl (+7 lines)
Line 0 Link Here
1
$DBversion = 'XXX';  # will be replaced by the RM
2
if( CheckVersion( $DBversion ) ) {
3
    $dbh->do("INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES ('ValidatePhoneNumber','','','Regex for validation of patron phone numbers.','Textarea')");
4
5
    SetVersion( $DBversion );
6
    print "Upgrade to $DBversion done (Bug 14620 - description)\n";
7
}
(-)a/installer/data/mysql/sysprefs.sql (+1 lines)
Lines 576-581 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
576
('UseQueryParser','0',NULL,'If enabled, try to use QueryParser for queries.','YesNo'),
576
('UseQueryParser','0',NULL,'If enabled, try to use QueryParser for queries.','YesNo'),
577
('UseTransportCostMatrix','0','','Use Transport Cost Matrix when filling holds','YesNo'),
577
('UseTransportCostMatrix','0','','Use Transport Cost Matrix when filling holds','YesNo'),
578
('UseWYSIWYGinSystemPreferences','0','','Show WYSIWYG editor when editing certain HTML system preferences.','YesNo'),
578
('UseWYSIWYGinSystemPreferences','0','','Show WYSIWYG editor when editing certain HTML system preferences.','YesNo'),
579
('ValidatePhoneNumber','','','Regex for validation of patron phone numbers.','Textarea'),
579
('viewISBD','1','','Allow display of ISBD view of bibiographic records','YesNo'),
580
('viewISBD','1','','Allow display of ISBD view of bibiographic records','YesNo'),
580
('viewLabeledMARC','0','','Allow display of labeled MARC view of bibiographic records','YesNo'),
581
('viewLabeledMARC','0','','Allow display of labeled MARC view of bibiographic records','YesNo'),
581
('viewMARC','1','','Allow display of MARC view of bibiographic records','YesNo'),
582
('viewMARC','1','','Allow display of MARC view of bibiographic records','YesNo'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref (+9 lines)
Lines 219-221 Patrons: Link Here
219
         - pref: FailedLoginAttempts
219
         - pref: FailedLoginAttempts
220
           class: integer
220
           class: integer
221
         - failed login attempts.
221
         - failed login attempts.
222
     -
223
         - Use the following regex /
224
         - pref: ValidatePhoneNumber
225
           type: textarea
226
           class: code
227
         - / to validate patrons' phone numbers.
228
         - Example ^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}$
229
         - (Source of example http://regexlib.com/REDetails.aspx?regexp_id=3009)
230
         - Leave blank to accept any phone number.
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt (+25 lines)
Lines 62-67 $(document).ready(function() { Link Here
62
        $(".toggler").toggle();
62
        $(".toggler").toggle();
63
    });
63
    });
64
64
65
66
    var MSG_INCORRECT_PHONE = _("Please enter a valid phone number.");
67
    $.validator.addMethod('phone', function(value) {
68
        value = value.trim();
69
        if (!value.trim()) {
70
            return 1;
71
        }
72
        else {
73
            return (value.match(/[% ValidatePhoneNumber %]/));
74
        }
75
    },
76
    MSG_INCORRECT_PHONE);
77
65
    $("#save_quick_add").click(function(){
78
    $("#save_quick_add").click(function(){
66
        $("#quick_add_form").validate();
79
        $("#quick_add_form").validate();
67
        if( $("#quick_add_form").valid()){
80
        if( $("#quick_add_form").valid()){
Lines 201-206 $(document).ready(function() { Link Here
201
            [% IF ERROR_bad_email_alternative %]
214
            [% IF ERROR_bad_email_alternative %]
202
                <li id="ERROR_bad_email_alternative">The alternative email is invalid.</li>
215
                <li id="ERROR_bad_email_alternative">The alternative email is invalid.</li>
203
            [% END %]
216
            [% END %]
217
            [% IF ERROR_bad_phone %]
218
                <li id="ERROR_bad_phone">The primary phone is invalid.</li>
219
            [% END %]
220
            [% IF ERROR_bad_phone_secondary %]
221
                <li id="ERROR_bad_phone_secondary">The secondary phone is invalid.</li>
222
            [% END %]
223
            [% IF ERROR_bad_phone_alternative %]
224
                <li id="ERROR_bad_phone_alternative">The alternative phone is invalid.</li>
225
            [% END %]
226
            [% IF ERROR_bad_smsnumber %]
227
                <li id="ERROR_bad_smsnumber">The SMS number is invalid.</li>
228
            [% END %]
204
			</ul>
229
			</ul>
205
		</div>
230
		</div>
206
	[% END %]
231
	[% END %]
(-)a/koha-tmpl/intranet-tmpl/prog/js/members.js (+28 lines)
Lines 333-338 $(document).ready(function(){ Link Here
333
            },
333
            },
334
            B_email: {
334
            B_email: {
335
                email: true
335
                email: true
336
            },
337
            phone: {
338
                phone: true
339
            },
340
            phonepro: {
341
                phone: true
342
            },
343
            mobile: {
344
                phone: true
345
            },
346
            SMSnumber: {
347
                phone: true
348
            },
349
            B_phone: {
350
                phone: true
336
            }
351
            }
337
        },
352
        },
338
        submitHandler: function(form) {
353
        submitHandler: function(form) {
Lines 341-350 $(document).ready(function(){ Link Here
341
                return false;
356
                return false;
342
            else
357
            else
343
                form.beenSubmitted = true;
358
                form.beenSubmitted = true;
359
                $("#email, #emailpro, #B_email").each(function(){
360
                    $(this).val($.trim($(this).val()));
361
                });
362
                $("#phone, #phonepro, #B_phone, #SMSnumber").each(function(){
363
                    $(this).val($.trim($(this).val()));
364
                });
344
                form.submit();
365
                form.submit();
345
            }
366
            }
346
    });
367
    });
347
368
369
    $("#email, #emailpro, #B_email").each(function(){
370
        $(this).val($.trim($(this).val()));
371
    });
372
    $("#phone, #phonepro, #B_phone, #SMSnumber").each(function(){
373
        $(this).val($.trim($(this).val()));
374
    });
375
348
    var mrform = $("#manual_restriction_form");
376
    var mrform = $("#manual_restriction_form");
349
    var mrlink = $("#add_manual_restriction");
377
    var mrlink = $("#add_manual_restriction");
350
    mrform.hide();
378
    mrform.hide();
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-memberentry.tt (+37 lines)
Lines 64-69 Link Here
64
                                [% IF field == "email" %]<li>Contact information: <a href="#borrower_email">primary email address</a></li>[% END %]
64
                                [% IF field == "email" %]<li>Contact information: <a href="#borrower_email">primary email address</a></li>[% END %]
65
                                [% IF field == "emailpro" %]<li>Contact information: <a href="#borrower_emailpro">secondary email address</a></li>[% END %]
65
                                [% IF field == "emailpro" %]<li>Contact information: <a href="#borrower_emailpro">secondary email address</a></li>[% END %]
66
                                [% IF field == "B_email" %]<li>Alternate address information: <a href="#borrower_B_email">email address</a></li>[% END %]
66
                                [% IF field == "B_email" %]<li>Alternate address information: <a href="#borrower_B_email">email address</a></li>[% END %]
67
                                [% IF field == "phone" %]<li>Contact information: <a href="#borrower_phone">primary phone</a></li>[% END %]
68
                                [% IF field == "phonepro" %]<li>Contact information: <a href="#borrower_phonepro">secondary phone</a></li>[% END %]
69
                                [% IF field == "mobile" %]<li>Contact information: <a href="#borrower_mobile">other phone</a></li>[% END %]
70
                                [% IF field == "B_phone" %]<li>Alternate address information: <a href="#borrower_B_phone">phone</a></li>[% END %]
67
                                [% IF field == "password_match" %]<li>Passwords do not match! <a href="#password">password</a></li>[% END %]
71
                                [% IF field == "password_match" %]<li>Passwords do not match! <a href="#password">password</a></li>[% END %]
68
                                [% IF field == "password_invalid" %]<li>Password does not meet minimum requirements! <a href="#password">password</a></li>[% END %]
72
                                [% IF field == "password_invalid" %]<li>Password does not meet minimum requirements! <a href="#password">password</a></li>[% END %]
69
                                [% IF field == "password_spaces" %]<li>Password contains leading and/or trailing spaces! <a href="#password">password</a></li>[% END %]
73
                                [% IF field == "password_spaces" %]<li>Password contains leading and/or trailing spaces! <a href="#password">password</a></li>[% END %]
Lines 943-948 Link Here
943
                $('label.required').removeClass('required');
947
                $('label.required').removeClass('required');
944
            [% END %]
948
            [% END %]
945
949
950
            var MSG_INCORRECT_PHONE = _("Please enter a valid phone number.");
951
            $.validator.addMethod('phone', function(value) {
952
                if (!value.trim()) {
953
                    return 1;
954
                }
955
                else {
956
                    return (value.match(/[% ValidatePhoneNumber %]/));
957
                }
958
            },
959
            MSG_INCORRECT_PHONE);
960
946
            $("#memberentry-form").validate({
961
            $("#memberentry-form").validate({
947
                rules: {
962
                rules: {
948
                    borrower_email: {
963
                    borrower_email: {
Lines 953-958 Link Here
953
                    },
968
                    },
954
                    borrower_B_email: {
969
                    borrower_B_email: {
955
                        email: true
970
                        email: true
971
                    },
972
                    borrower_phone: {
973
                        phone: true
974
                    },
975
                    borrower_phonepro: {
976
                        phone: true
977
                    },
978
                    borrower_mobile: {
979
                        phone: true
980
                    },
981
                    borrower_B_phone: {
982
                        phone: true
956
                    }
983
                    }
957
                },
984
                },
958
                submitHandler: function(form) {
985
                submitHandler: function(form) {
Lines 961-966 Link Here
961
                    }
988
                    }
962
                    else {
989
                    else {
963
                        form.beenSubmitted = true;
990
                        form.beenSubmitted = true;
991
                        $("#borrower_email, #borrower_emailpro, #borrower_B_email").each(function(){
992
                            $(this).val($.trim($(this).val()));
993
                        });
994
                        $("#borrower_phone, #borrower_phonepro, #borrower_B_phone").each(function(){
995
                            $(this).val($.trim($(this).val()));
996
                        });
964
                        form.submit();
997
                        form.submit();
965
                    }
998
                    }
966
                },
999
                },
Lines 975-980 Link Here
975
                }
1008
                }
976
            });
1009
            });
977
1010
1011
            $("#borrower_email, #borrower_emailpro, #borrower_B_email, #borrower_phone, #borrower_phonepro, #borrower_B_phone").on("change", function(){
1012
                $(this).val($.trim($(this).val()));
1013
            });
1014
978
            [% IF borrower.guarantorid && !Koha.Preference('OPACPrivacy') && Koha.Preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') %]
1015
            [% IF borrower.guarantorid && !Koha.Preference('OPACPrivacy') && Koha.Preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') %]
979
                $('#update_privacy_guarantor_checkouts').click( function() {
1016
                $('#update_privacy_guarantor_checkouts').click( function() {
980
                    $.post( "/cgi-bin/koha/svc/patron/show_checkouts_to_relatives", { privacy_guarantor_checkouts: $('#privacy_guarantor_checkouts').val() }, null, 'json')
1017
                    $.post( "/cgi-bin/koha/svc/patron/show_checkouts_to_relatives", { privacy_guarantor_checkouts: $('#privacy_guarantor_checkouts').val() }, null, 'json')
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-messaging.tt (+44 lines)
Lines 28-33 Link Here
28
                    [% IF ( settings_updated ) %]
28
                    [% IF ( settings_updated ) %]
29
                        <div class="alert alert-success"><h4>Settings updated</h4></div>
29
                        <div class="alert alert-success"><h4>Settings updated</h4></div>
30
                    [% END %]
30
                    [% END %]
31
                    [% IF ( invalid_smsnumber ) %]
32
                        <div class="alert alert-error"><h4>Invalid SMS number</h4></div>
33
                    [% END %]
31
                    <form action="/cgi-bin/koha/opac-messaging.pl" method="get" name="opacmessaging">
34
                    <form action="/cgi-bin/koha/opac-messaging.pl" method="get" name="opacmessaging">
32
                        <input type="hidden" name="modify" value="yes" />
35
                        <input type="hidden" name="modify" value="yes" />
33
36
Lines 184-190 Link Here
184
      }
187
      }
185
    });
188
    });
186
    $("#info_digests").tooltip();
189
    $("#info_digests").tooltip();
190
191
    var MSG_INCORRECT_PHONE = _("Please enter a valid phone number.");
192
    $.validator.addMethod('phone', function(value) {
193
        if (!value.trim()) {
194
            return 1;
195
        }
196
        else {
197
            return (value.match(/[% ValidatePhoneNumber %]/));
198
        }
199
    },
200
    MSG_INCORRECT_PHONE);
201
202
    $("form[name='opacmessaging']").validate({
203
        rules: {
204
            SMSnumber: {
205
                phone: true
206
            }
207
        },
208
        submitHandler: function(form) {
209
            $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
210
            if (form.beenSubmitted) {
211
                return false;
212
            }
213
            else {
214
                form.beenSubmitted = true;
215
                $("#SMSnumber").each(function(){
216
                    $(this).val($.trim($(this).val()));
217
                });
218
                form.submit();
219
            }
220
        },
221
        errorPlacement: function(error, element) {
222
            error.insertAfter(element.closest("li")).wrap('<li></li>');
223
            error.addClass('alert-error');
224
            error.width("auto");
225
        }
226
    });
227
    $("#SMSnumber").on("change", function(){
228
        $(this).val($.trim($(this).val()));
229
    });
187
  });
230
  });
188
//]]>
231
//]]>
189
</script>
232
</script>
233
<script type="text/javascript" src="/opac-tmpl/bootstrap/lib/jquery/plugins/jquery.validate.min.js"></script>
190
[% END %]
234
[% END %]
(-)a/members/memberentry.pl (-15 / +17 lines)
Lines 47-53 use Koha::Patron::Categories; Link Here
47
use Koha::Patron::HouseboundRole;
47
use Koha::Patron::HouseboundRole;
48
use Koha::Patron::HouseboundRoles;
48
use Koha::Patron::HouseboundRoles;
49
use Koha::Token;
49
use Koha::Token;
50
use Email::Valid;
50
use Koha::Validation;
51
use Module::Load;
51
use Module::Load;
52
if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
52
if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
53
    load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
53
    load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
Lines 363-381 if ($op eq 'save' || $op eq 'insert'){ Link Here
363
  push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
363
  push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
364
364
365
  # Validate emails
365
  # Validate emails
366
  my $emailprimary = $input->param('email');
366
  push (@errors, "ERROR_bad_email") if ($input->param('email') && !Koha::Validation::email($input->param('email')));
367
  my $emailsecondary = $input->param('emailpro');
367
  push (@errors, "ERROR_bad_email_secondary") if ($input->param('emailpro') && !Koha::Validation::email($input->param('emailpro')));
368
  my $emailalt = $input->param('B_email');
368
  push (@errors, "ERROR_bad_email_alternative") if ($input->param('B_email') && !Koha::Validation::email($input->param('B_email')));
369
369
  # Validate phone numbers
370
  if ($emailprimary) {
370
  push (@errors, "ERROR_bad_phone") if ($input->param('phone') && !Koha::Validation::phone($input->param('phone')));
371
      push (@errors, "ERROR_bad_email") if (!Email::Valid->address($emailprimary));
371
  push (@errors, "ERROR_bad_phone_secondary") if ($input->param('phonepro') && !Koha::Validation::phone($input->param('phonepro')));
372
  }
372
  push (@errors, "ERROR_bad_phone_alternative") if ($input->param('B_phone') && !Koha::Validation::phone($input->param('B_phone')));
373
  if ($emailsecondary) {
374
      push (@errors, "ERROR_bad_email_secondary") if (!Email::Valid->address($emailsecondary));
375
  }
376
  if ($emailalt) {
377
      push (@errors, "ERROR_bad_email_alternative") if (!Email::Valid->address($emailalt));
378
  }
379
373
380
  if (C4::Context->preference('ExtendedPatronAttributes')) {
374
  if (C4::Context->preference('ExtendedPatronAttributes')) {
381
    $extended_patron_attributes = parse_extended_patron_attributes($input);
375
    $extended_patron_attributes = parse_extended_patron_attributes($input);
Lines 403-409 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') Link Here
403
# BZ 14683: Do not mixup mobile [read: other phone] with smsalertnumber
397
# BZ 14683: Do not mixup mobile [read: other phone] with smsalertnumber
404
my $sms = $input->param('SMSnumber');
398
my $sms = $input->param('SMSnumber');
405
if ( defined $sms ) {
399
if ( defined $sms ) {
406
    $newdata{smsalertnumber} = $sms;
400
    if (Koha::Validation::phone($sms)){
401
        $newdata{smsalertnumber} = $sms;
402
    } else {
403
        push (@errors, "ERROR_bad_smsnumber");
404
    }
407
}
405
}
408
406
409
###  Error checks should happen before this line.
407
###  Error checks should happen before this line.
Lines 720-725 if (C4::Context->preference('ExtendedPatronAttributes')) { Link Here
720
    patron_attributes_form($template, $borrowernumber);
718
    patron_attributes_form($template, $borrowernumber);
721
}
719
}
722
720
721
$template->param(
722
    ValidatePhoneNumber  => C4::Context->preference('ValidatePhoneNumber') || '.*',
723
);
724
723
if (C4::Context->preference('EnhancedMessagingPreferences')) {
725
if (C4::Context->preference('EnhancedMessagingPreferences')) {
724
    if ($op eq 'add') {
726
    if ($op eq 'add') {
725
        C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
727
        C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
(-)a/opac/opac-memberentry.pl (-4 / +17 lines)
Lines 29-35 use C4::Members; Link Here
29
use C4::Members::Attributes qw( GetBorrowerAttributes );
29
use C4::Members::Attributes qw( GetBorrowerAttributes );
30
use C4::Form::MessagingPreferences;
30
use C4::Form::MessagingPreferences;
31
use C4::Scrubber;
31
use C4::Scrubber;
32
use Email::Valid;
33
use Koha::DateUtils;
32
use Koha::DateUtils;
34
use Koha::Libraries;
33
use Koha::Libraries;
35
use Koha::Patron::Attribute::Types;
34
use Koha::Patron::Attribute::Types;
Lines 39-44 use Koha::Patron::Modification; Link Here
39
use Koha::Patron::Modifications;
38
use Koha::Patron::Modifications;
40
use Koha::Patrons;
39
use Koha::Patrons;
41
use Koha::Token;
40
use Koha::Token;
41
use Koha::Validation;
42
42
43
my $cgi = new CGI;
43
my $cgi = new CGI;
44
my $dbh = C4::Context->dbh;
44
my $dbh = C4::Context->dbh;
Lines 88-93 $template->param( Link Here
88
    mandatory         => $mandatory,
88
    mandatory         => $mandatory,
89
    libraries         => \@libraries,
89
    libraries         => \@libraries,
90
    OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
90
    OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
91
    ValidatePhoneNumber   => C4::Context->preference('ValidatePhoneNumber') || '.*',
91
);
92
);
92
93
93
my $attributes = ParsePatronAttributes($borrowernumber,$cgi);
94
my $attributes = ParsePatronAttributes($borrowernumber,$cgi);
Lines 390-396 sub CheckForInvalidFields { Link Here
390
    my $borrower = shift;
391
    my $borrower = shift;
391
    my @invalidFields;
392
    my @invalidFields;
392
    if ($borrower->{'email'}) {
393
    if ($borrower->{'email'}) {
393
        unless ( Email::Valid->address($borrower->{'email'}) ) {
394
        unless ( Koha::Validation::email($borrower->{'email'}) ) {
394
            push(@invalidFields, "email");
395
            push(@invalidFields, "email");
395
        } elsif ( C4::Context->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
396
        } elsif ( C4::Context->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
396
            my $patrons_with_same_email = Koha::Patrons->search(
397
            my $patrons_with_same_email = Koha::Patrons->search(
Lines 410-419 sub CheckForInvalidFields { Link Here
410
        }
411
        }
411
    }
412
    }
412
    if ($borrower->{'emailpro'}) {
413
    if ($borrower->{'emailpro'}) {
413
        push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
414
        push(@invalidFields, "emailpro") if (!Koha::Validation::email($borrower->{'emailpro'}));
414
    }
415
    }
415
    if ($borrower->{'B_email'}) {
416
    if ($borrower->{'B_email'}) {
416
        push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
417
        push(@invalidFields, "B_email") if (!Koha::Validation::email($borrower->{'B_email'}));
418
    }
419
    if ($borrower->{'mobile'}) {
420
        push(@invalidFields, "mobile") if (!Koha::Validation::phone($borrower->{'mobile'}));
421
    }
422
    if ($borrower->{'phone'}) {
423
        push(@invalidFields, "phone") if (!Koha::Validation::phone($borrower->{'phone'}));
424
    }
425
    if ($borrower->{'phonepro'}) {
426
        push(@invalidFields, "phonepro") if (!Koha::Validation::phone($borrower->{'phonepro'}));
427
    }
428
    if ($borrower->{'B_phone'}) {
429
        push(@invalidFields, "B_phone") if (!Koha::Validation::phone($borrower->{'B_phone'}));
417
    }
430
    }
418
    if ( defined $borrower->{'password'}
431
    if ( defined $borrower->{'password'}
419
        and $borrower->{'password'} ne $borrower->{'password2'} )
432
        and $borrower->{'password'} ne $borrower->{'password2'} )
(-)a/opac/opac-messaging.pl (-1 / +12 lines)
Lines 31-36 use C4::Members; Link Here
31
use C4::Members::Messaging;
31
use C4::Members::Messaging;
32
use C4::Form::MessagingPreferences;
32
use C4::Form::MessagingPreferences;
33
use Koha::SMS::Providers;
33
use Koha::SMS::Providers;
34
use Koha::Validation;
34
35
35
my $query = CGI->new();
36
my $query = CGI->new();
36
37
Lines 53-62 my ( $template, $borrowernumber, $cookie ) = get_template_and_user( Link Here
53
my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
54
my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
54
my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
55
my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
55
56
57
my $validate_phone = C4::Context->preference('ValidatePhoneNumber');
58
59
$template->param(
60
    ValidatePhoneNumber    => $validate_phone || '.*',
61
);
62
56
if ( defined $query->param('modify') && $query->param('modify') eq 'yes' ) {
63
if ( defined $query->param('modify') && $query->param('modify') eq 'yes' ) {
57
    my $sms = $query->param('SMSnumber');
64
    my $sms = $query->param('SMSnumber');
58
    my $sms_provider_id = $query->param('sms_provider_id');
65
    my $sms_provider_id = $query->param('sms_provider_id');
59
    if ( defined $sms && ( $borrower->{'smsalertnumber'} // '' ) ne $sms
66
67
    my $valid_sms = Koha::Validation::phone($sms);
68
    $template->param( invalid_smsnumber => 1 ) unless $valid_sms;
69
70
    if ( defined $sms && $valid_sms && ( $borrower->{'smsalertnumber'} // '' ) ne $sms
60
            or ( $borrower->{sms_provider_id} // '' ) ne $sms_provider_id ) {
71
            or ( $borrower->{sms_provider_id} // '' ) ne $sms_provider_id ) {
61
        ModMember(
72
        ModMember(
62
            borrowernumber  => $borrowernumber,
73
            borrowernumber  => $borrowernumber,
(-)a/t/db_dependent/Koha/Validation.t (-1 / +76 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# Copyright 2017 Koha-Suomi Oy
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use Test::More tests => 3;
23
24
use t::lib::Mocks;
25
26
BEGIN {
27
    use_ok('Koha::Validation');
28
}
29
30
subtest 'email() tests' => sub {
31
    plan tests => 2;
32
33
    is(Koha::Validation::email('test'), 0, "'test' is invalid e-mail address'");
34
    is(Koha::Validation::email('test@example.com'), 1, '\'test@example.com\' is '
35
                               .'valid e-mail address');
36
};
37
38
subtest 'phone() tests' => sub {
39
    plan tests => 3;
40
41
    t::lib::Mocks::mock_preference('ValidatePhoneNumber', '');
42
43
    is(Koha::Validation::phone('test'), 1, 'Phone number validation is switched '
44
       .'off, so \'test\' is a valid phone number');
45
46
    # An example: Finnish Phone number validation regex
47
    subtest 'Finnish phone number validation regex' => sub {
48
        t::lib::Mocks::mock_preference('ValidatePhoneNumber',
49
            '^((90[0-9]{3})?0|\+358\s?)(?!(100|20(0|2(0|[2-3])|9[8-9])|300|600|70'
50
           .'0|708|75(00[0-3]|(1|2)\d{2}|30[0-2]|32[0-2]|75[0-2]|98[0-2])))(4|50|'
51
           .'10[1-9]|20(1|2(1|[4-9])|[3-9])|29|30[1-9]|71|73|75(00[3-9]|30[3-9]|3'
52
           .'2[3-9]|53[3-9]|83[3-9])|2|3|5|6|8|9|1[3-9])\s?(\d\s?){4,19}\d$'
53
        );
54
55
        is(Koha::Validation::phone('1234'), 0, '1234 is invalid phone number.');
56
        is(Koha::Validation::phone('+358501234567'), 1, '+358501234567 is valid '
57
           .'phone number.');
58
        is(Koha::Validation::phone('+1-202-555-0198'), 0, '+1-202-555-0198 is '
59
          .'invalid phone number.');
60
    };
61
62
    subtest 'International phone number validation regex' => sub {
63
        t::lib::Mocks::mock_preference('ValidatePhoneNumber',
64
            '^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]'
65
           .'{1,12}){1,2}$'
66
        );
67
68
        is(Koha::Validation::phone('nope'), 0, 'nope is invalid phone number.');
69
        is(Koha::Validation::phone('1234'), 1, '1234 is valid phone number.');
70
        is(Koha::Validation::phone('+358501234567'), 1, '+358501234567 is valid '
71
           .'phone number.');
72
        is(Koha::Validation::phone('+1-202-555-0198'), 1, '+1-202-555-0198 is '
73
          .'valid phone number.');
74
    };
75
76
};

Return to bug 14620