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 563-568 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
563
('UseQueryParser','0',NULL,'If enabled, try to use QueryParser for queries.','YesNo'),
563
('UseQueryParser','0',NULL,'If enabled, try to use QueryParser for queries.','YesNo'),
564
('UseTransportCostMatrix','0','','Use Transport Cost Matrix when filling holds','YesNo'),
564
('UseTransportCostMatrix','0','','Use Transport Cost Matrix when filling holds','YesNo'),
565
('UseWYSIWYGinSystemPreferences','0','','Show WYSIWYG editor when editing certain HTML system preferences.','YesNo'),
565
('UseWYSIWYGinSystemPreferences','0','','Show WYSIWYG editor when editing certain HTML system preferences.','YesNo'),
566
('ValidatePhoneNumber','','','Regex for validation of patron phone numbers.','Textarea'),
566
('viewISBD','1','','Allow display of ISBD view of bibiographic records','YesNo'),
567
('viewISBD','1','','Allow display of ISBD view of bibiographic records','YesNo'),
567
('viewLabeledMARC','0','','Allow display of labeled MARC view of bibiographic records','YesNo'),
568
('viewLabeledMARC','0','','Allow display of labeled MARC view of bibiographic records','YesNo'),
568
('viewMARC','1','','Allow display of MARC view of bibiographic records','YesNo'),
569
('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 212-214 Patrons: Link Here
212
               yes: Do
212
               yes: Do
213
               no: "Don't"
213
               no: "Don't"
214
         - search the Norwegian national patron database after a local search result was found.
214
         - search the Norwegian national patron database after a local search result was found.
215
     -
216
         - Use the following regex /
217
         - pref: ValidatePhoneNumber
218
           type: textarea
219
           class: code
220
         - / to validate patrons' phone numbers.
221
         - Example ^((\+)?[1-9]{1,2})?([-\s\.])?((\(\d{1,4}\))|\d{1,4})(([-\s\.])?[0-9]{1,12}){1,2}$
222
         - (Source of example http://regexlib.com/REDetails.aspx?regexp_id=3009)
223
         - Leave blank to accept any phone number.
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt (+25 lines)
Lines 64-69 $(document).ready(function() { Link Here
64
        $(".toggler").toggle();
64
        $(".toggler").toggle();
65
    });
65
    });
66
66
67
    
68
    var MSG_INCORRECT_PHONE = _("Please enter a valid phone number.");
69
    $.validator.addMethod('phone', function(value) {
70
        value = value.trim();
71
        if (!value.trim()) {
72
            return 1;
73
        }
74
        else {
75
            return (value.match(/[% ValidatePhoneNumber %]/));
76
        }
77
    },
78
    MSG_INCORRECT_PHONE);
79
67
    $("#save_quick_add").click(function(){
80
    $("#save_quick_add").click(function(){
68
        $("#quick_add_form").validate();
81
        $("#quick_add_form").validate();
69
        if( $("#quick_add_form").valid()){
82
        if( $("#quick_add_form").valid()){
Lines 203-208 $(document).ready(function() { Link Here
203
            [% IF ERROR_bad_email_alternative %]
216
            [% IF ERROR_bad_email_alternative %]
204
                <li id="ERROR_bad_email_alternative">The alternative email is invalid.</li>
217
                <li id="ERROR_bad_email_alternative">The alternative email is invalid.</li>
205
            [% END %]
218
            [% END %]
219
            [% IF ERROR_bad_phone %]
220
                <li id="ERROR_bad_phone">The primary phone is invalid.</li>
221
            [% END %]
222
            [% IF ERROR_bad_phone_secondary %]
223
                <li id="ERROR_bad_phone_secondary">The secondary phone is invalid.</li>
224
            [% END %]
225
            [% IF ERROR_bad_phone_alternative %]
226
                <li id="ERROR_bad_phone_alternative">The alternative phone is invalid.</li>
227
            [% END %]
228
            [% IF ERROR_bad_smsnumber %]
229
                <li id="ERROR_bad_smsnumber">The SMS number is invalid.</li>
230
            [% END %]
206
			</ul>
231
			</ul>
207
		</div>
232
		</div>
208
	[% END %]
233
	[% 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 1001-1006 Link Here
1001
                $( "#borrower_dateofbirth" ).datepicker({ yearRange: "c-120:c" });
1005
                $( "#borrower_dateofbirth" ).datepicker({ yearRange: "c-120:c" });
1002
            [% END %]
1006
            [% END %]
1003
1007
1008
            var MSG_INCORRECT_PHONE = _("Please enter a valid phone number.");
1009
            $.validator.addMethod('phone', function(value) {
1010
                if (!value.trim()) {
1011
                    return 1;
1012
                }
1013
                else {
1014
                    return (value.match(/[% ValidatePhoneNumber %]/));
1015
                }
1016
            },
1017
            MSG_INCORRECT_PHONE);
1018
1004
            $("#memberentry-form").validate({
1019
            $("#memberentry-form").validate({
1005
                rules: {
1020
                rules: {
1006
                    borrower_email: {
1021
                    borrower_email: {
Lines 1011-1016 Link Here
1011
                    },
1026
                    },
1012
                    borrower_B_email: {
1027
                    borrower_B_email: {
1013
                        email: true
1028
                        email: true
1029
                    },
1030
                    borrower_phone: {
1031
                        phone: true
1032
                    },
1033
                    borrower_phonepro: {
1034
                        phone: true
1035
                    },
1036
                    borrower_mobile: {
1037
                        phone: true
1038
                    },
1039
                    borrower_B_phone: {
1040
                        phone: true
1014
                    }
1041
                    }
1015
                },
1042
                },
1016
                submitHandler: function(form) {
1043
                submitHandler: function(form) {
Lines 1019-1024 Link Here
1019
                    }
1046
                    }
1020
                    else {
1047
                    else {
1021
                        form.beenSubmitted = true;
1048
                        form.beenSubmitted = true;
1049
                        $("#borrower_email, #borrower_emailpro, #borrower_B_email").each(function(){
1050
                            $(this).val($.trim($(this).val()));
1051
                        });
1052
                        $("#borrower_phone, #borrower_phonepro, #borrower_B_phone").each(function(){
1053
                            $(this).val($.trim($(this).val()));
1054
                        });
1022
                        form.submit();
1055
                        form.submit();
1023
                    }
1056
                    }
1024
                },
1057
                },
Lines 1033-1038 Link Here
1033
                }
1066
                }
1034
            });
1067
            });
1035
1068
1069
            $("#borrower_email, #borrower_emailpro, #borrower_B_email, #borrower_phone, #borrower_phonepro, #borrower_B_phone").on("change", function(){
1070
                $(this).val($.trim($(this).val()));
1071
            });
1072
1036
            [% IF borrower.guarantorid && !Koha.Preference('OPACPrivacy') && Koha.Preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') %]
1073
            [% IF borrower.guarantorid && !Koha.Preference('OPACPrivacy') && Koha.Preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') %]
1037
                $('#update_privacy_guarantor_checkouts').click( function() {
1074
                $('#update_privacy_guarantor_checkouts').click( function() {
1038
                    $.post( "/cgi-bin/koha/svc/patron/show_checkouts_to_relatives", { privacy_guarantor_checkouts: $('#privacy_guarantor_checkouts').val() }, null, 'json')
1075
                    $.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 174-180 Link Here
174
      }
177
      }
175
    });
178
    });
176
    $("#info_digests").tooltip();
179
    $("#info_digests").tooltip();
180
181
    var MSG_INCORRECT_PHONE = _("Please enter a valid phone number.");
182
    $.validator.addMethod('phone', function(value) {
183
        if (!value.trim()) {
184
            return 1;
185
        }
186
        else {
187
            return (value.match(/[% ValidatePhoneNumber %]/));
188
        }
189
    },
190
    MSG_INCORRECT_PHONE);
191
192
    $("form[name='opacmessaging']").validate({
193
        rules: {
194
            SMSnumber: {
195
                phone: true
196
            }
197
        },
198
        submitHandler: function(form) {
199
            $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
200
            if (form.beenSubmitted) {
201
                return false;
202
            }
203
            else {
204
                form.beenSubmitted = true;
205
                $("#SMSnumber").each(function(){
206
                    $(this).val($.trim($(this).val()));
207
                });
208
                form.submit();
209
            }
210
        },
211
        errorPlacement: function(error, element) {
212
            error.insertAfter(element.closest("li")).wrap('<li></li>');
213
            error.addClass('alert-error');
214
            error.width("auto");
215
        }
216
    });
217
    $("#SMSnumber").on("change", function(){
218
        $(this).val($.trim($(this).val()));
219
    });
177
  });
220
  });
178
//]]>
221
//]]>
179
</script>
222
</script>
223
<script type="text/javascript" src="/opac-tmpl/bootstrap/lib/jquery/plugins/jquery.validate.min.js"></script>
180
[% END %]
224
[% END %]
(-)a/members/memberentry.pl (-15 / +17 lines)
Lines 46-52 use Koha::Patron::Categories; Link Here
46
use Koha::Patron::HouseboundRole;
46
use Koha::Patron::HouseboundRole;
47
use Koha::Patron::HouseboundRoles;
47
use Koha::Patron::HouseboundRoles;
48
use Koha::Token;
48
use Koha::Token;
49
use Email::Valid;
49
use Koha::Validation;
50
use Module::Load;
50
use Module::Load;
51
if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
51
if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
52
    load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
52
    load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
Lines 350-368 if ($op eq 'save' || $op eq 'insert'){ Link Here
350
  push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
350
  push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
351
351
352
  # Validate emails
352
  # Validate emails
353
  my $emailprimary = $input->param('email');
353
  push (@errors, "ERROR_bad_email") if ($input->param('email') && !Koha::Validation::email($input->param('email')));
354
  my $emailsecondary = $input->param('emailpro');
354
  push (@errors, "ERROR_bad_email_secondary") if ($input->param('emailpro') && !Koha::Validation::email($input->param('emailpro')));
355
  my $emailalt = $input->param('B_email');
355
  push (@errors, "ERROR_bad_email_alternative") if ($input->param('B_email') && !Koha::Validation::email($input->param('B_email')));
356
356
  # Validate phone numbers
357
  if ($emailprimary) {
357
  push (@errors, "ERROR_bad_phone") if ($input->param('phone') && !Koha::Validation::phone($input->param('phone')));
358
      push (@errors, "ERROR_bad_email") if (!Email::Valid->address($emailprimary));
358
  push (@errors, "ERROR_bad_phone_secondary") if ($input->param('phonepro') && !Koha::Validation::phone($input->param('phonepro')));
359
  }
359
  push (@errors, "ERROR_bad_phone_alternative") if ($input->param('B_phone') && !Koha::Validation::phone($input->param('B_phone')));
360
  if ($emailsecondary) {
361
      push (@errors, "ERROR_bad_email_secondary") if (!Email::Valid->address($emailsecondary));
362
  }
363
  if ($emailalt) {
364
      push (@errors, "ERROR_bad_email_alternative") if (!Email::Valid->address($emailalt));
365
  }
366
360
367
  if (C4::Context->preference('ExtendedPatronAttributes')) {
361
  if (C4::Context->preference('ExtendedPatronAttributes')) {
368
    $extended_patron_attributes = parse_extended_patron_attributes($input);
362
    $extended_patron_attributes = parse_extended_patron_attributes($input);
Lines 390-396 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') Link Here
390
# BZ 14683: Do not mixup mobile [read: other phone] with smsalertnumber
384
# BZ 14683: Do not mixup mobile [read: other phone] with smsalertnumber
391
my $sms = $input->param('SMSnumber');
385
my $sms = $input->param('SMSnumber');
392
if ( defined $sms ) {
386
if ( defined $sms ) {
393
    $newdata{smsalertnumber} = $sms;
387
    if (Koha::Validation::phone($sms)){
388
        $newdata{smsalertnumber} = $sms;
389
    } else {
390
        push (@errors, "ERROR_bad_smsnumber");
391
    }
394
}
392
}
395
393
396
###  Error checks should happen before this line.
394
###  Error checks should happen before this line.
Lines 707-712 if (C4::Context->preference('ExtendedPatronAttributes')) { Link Here
707
    patron_attributes_form($template, $borrowernumber);
705
    patron_attributes_form($template, $borrowernumber);
708
}
706
}
709
707
708
$template->param(
709
    ValidatePhoneNumber  => C4::Context->preference('ValidatePhoneNumber') || '.*',
710
);
711
710
if (C4::Context->preference('EnhancedMessagingPreferences')) {
712
if (C4::Context->preference('EnhancedMessagingPreferences')) {
711
    if ($op eq 'add') {
713
    if ($op eq 'add') {
712
        C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
714
        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 388-394 sub CheckForInvalidFields { Link Here
388
    my $borrower = shift;
389
    my $borrower = shift;
389
    my @invalidFields;
390
    my @invalidFields;
390
    if ($borrower->{'email'}) {
391
    if ($borrower->{'email'}) {
391
        unless ( Email::Valid->address($borrower->{'email'}) ) {
392
        unless ( Koha::Validation::email($borrower->{'email'}) ) {
392
            push(@invalidFields, "email");
393
            push(@invalidFields, "email");
393
        } elsif ( C4::Context->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
394
        } elsif ( C4::Context->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
394
            my $patrons_with_same_email = Koha::Patrons->search( { email => $borrower->{email} })->count;
395
            my $patrons_with_same_email = Koha::Patrons->search( { email => $borrower->{email} })->count;
Lines 398-407 sub CheckForInvalidFields { Link Here
398
        }
399
        }
399
    }
400
    }
400
    if ($borrower->{'emailpro'}) {
401
    if ($borrower->{'emailpro'}) {
401
        push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
402
        push(@invalidFields, "emailpro") if (!Koha::Validation::email($borrower->{'emailpro'}));
402
    }
403
    }
403
    if ($borrower->{'B_email'}) {
404
    if ($borrower->{'B_email'}) {
404
        push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
405
        push(@invalidFields, "B_email") if (!Koha::Validation::email($borrower->{'B_email'}));
406
    }
407
    if ($borrower->{'mobile'}) {
408
        push(@invalidFields, "mobile") if (!Koha::Validation::phone($borrower->{'mobile'}));
409
    }
410
    if ($borrower->{'phone'}) {
411
        push(@invalidFields, "phone") if (!Koha::Validation::phone($borrower->{'phone'}));
412
    }
413
    if ($borrower->{'phonepro'}) {
414
        push(@invalidFields, "phonepro") if (!Koha::Validation::phone($borrower->{'phonepro'}));
415
    }
416
    if ($borrower->{'B_phone'}) {
417
        push(@invalidFields, "B_phone") if (!Koha::Validation::phone($borrower->{'B_phone'}));
405
    }
418
    }
406
    if ( defined $borrower->{'password'}
419
    if ( defined $borrower->{'password'}
407
        and $borrower->{'password'} ne $borrower->{'password2'} )
420
        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 = C4::Members::GetMember( borrowernumber => $borrowernumber );
54
my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber );
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