Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_valid() returns unexpected validation when /^\d+$/ was set #6

Open
worthmine opened this issue Jul 28, 2020 · 0 comments
Open

is_valid() returns unexpected validation when /^\d+$/ was set #6

worthmine opened this issue Jul 28, 2020 · 0 comments

Comments

@worthmine
Copy link

I have made my validation routine like below:

sub optimize {
    my $phone = shift;
    $phone = alnum_z2h($phone); # 全角数字を半角に
    $phone =~ s/[^\d\-\s]+/ /g; # 関係ない文字の削除 
    $phone =~ s/[\s\-]+/ /g;    # デリミタを統一
    
    return '電話番号をもう一度確認してください。'
      unless Number::Phone::JP->new->set_number($phone)->is_valid();
    return $phone;
}

But it returns message with some warn like below:

The number is invalid telephone number. at xxxx.pl line xx.
Any number was not set at xxxx.pl line xx.
電話番号をもう一度確認してください。

It seems that numbers with no space( /^\d+$/ ) return same errors.
the full of my test code is here:

use feature qw(say);
use strict;
use warnings;

use utf8;
use Lingua::JA::Regular::Unicode qw(alnum_z2h);
use Number::Phone::JP qw(Freedial);

binmode STDOUT,':utf8';
binmode STDERR, ':utf8';

my $do_divide = 0;
my $phone = optimize('0120893893'); # 全角数字(ハイフン無し)
say $phone; # 0120 893 893 を期待
#The number is invalid telephone number. at test.pl line 36.
#Any number was not set at test.pl line 36.
#電話番号をもう一度確認してください。

say optimize('0120893893');     # 半角数字でも同じ結果
say optimize('0120 893 893');   # スペースを入れると解決
$do_divide = 1;                 # またはスペースを挿入する処理を呼び出す
say optimize('0120893893');     # と解決

exit;

sub optimize {
    my $phone = shift;
    $phone = alnum_z2h($phone); # 全角数字を半角に
    $phone =~ s/[^\d\-\s]+/ /g; # 関係ない文字の削除 
    $phone =~ s/[\s\-]+/ /g;    # デリミタを統一
    if( $do_divide and $phone !~ / / ) {       # 連番を分離する
        $phone =~ /^0120(\d{3})(\d{3})$/;
        $phone = "0120 $1 $2";
    }
    
    return '電話番号をもう一度確認してください。'
      unless Number::Phone::JP->new->set_number($phone)->is_valid();
    return $phone;
}

Does it deserve to write a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant