【问题标题】:check field formmail检查字段formmail
【发布时间】:2010-09-29 12:03:57
【问题描述】:

我正在尝试改变这一点:

foreach $require (@Required) {

        # If the required field is the email field, the syntax of the email  #
        # address if checked to make sure it passes a valid syntax.          #
        if ($require eq 'email' && !&check_email($Config{$require})) {
            push(@error,$require);
        }

//////////////////////////////////////////////////////////////////////////////////

sub check_email {
    # Initialize local email variable with input to subroutine.              #
    $email = $_[0];

    # If the e-mail address contains:                                        #
    if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||

        # the e-mail address contains an invalid syntax.  Or, if the         #
        # syntax does not match the following regular expression pattern     #
        # it fails basic syntax verification.                                #

        $email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9]+)(\]?)$/) {

        # Basic syntax requires:  one or more characters before the @ sign,  #
        # followed by an optional '[', then any number of letters, numbers,  #
        # dashes or periods (valid domain/IP characters) ending in a period  #
        # and then 2 or 3 letters (for domain suffixes) or 1 to 3 numbers    #
        # (for IP addresses).  An ending bracket is also allowed as it is    #
        # valid syntax to have an email address like: user@[255.255.255.0]   #

        # Return a false value, since the e-mail address did not pass valid  #
        # syntax.                                                            #
        return 0;
    }

    else {

        # Return a true value, e-mail verification passed.                   #
        return 1;
    }
}

进入这个:

foreach $require (@Required) {


            if ($require eq 'fieldb' && !&check_fieldb($Config{$require})) {
                push(@error,$require);
            }

    ///////////////////////////////////////////////////////////////////////////////

    sub check_fieldb {

        # If field b is under 20% of field a:                                        #
        if ($fieldb <=($fielda/100)*20 ) {

            # Return a false value, since field b is less than 20% of field a
            return 0;
        }

        else {

            # Return a true value, fieldb verification passed.                   #
            return 1;
        }
    }

但它不起作用,总是返回为 0。 我该如何解决这个问题?

【问题讨论】:

  • !编码恐怖! - 全局变量!!哎哟哎哟我的眼睛!在将 $fielda$fieldb 作为参数传递给函数之前,不要考虑寻求帮助。
  • 另外,如果您有权访问 CPAN... 您应该使用 Email::Valid (search.cpan.org/~rjbs/Email-Valid-0.184/lib/Email/Valid.pm) 来检查
  • 我不想验证电子邮件字段,只是一个数字字段。至于全局变量,它不是我的脚本——我对 perl 了解不多
  • 神秘的“for-if 模式”出现了。
  • if语句下有一个elseif和else, ////是分开的两部分代码

标签: perl forms webforms validation


【解决方案1】:

如果不知道 $fielda 和 $fieldb 的值,就不可能确定出了什么问题。我的诊断是$fieldb小于等于($fielda/100)*20

您将一个值传递给 check_fieldb,但您从不使用它。你为什么通过它?正如评论者指出的那样,您应该将要检查的值传递给函数。 $fielda 和 $fieldb 是否保证在调用 check_fieldb 之前正确初始化?

你的意思是说

foreach my $require (@Required){
    if($require eq 'fieldb' && !check_fieldb($value_of_fielda, $value_of_fieldb)){
        push(@error, $require);
    }
}

# ... later ...

sub check_fieldb($$){
    my $fielda = shift;
    my $fieldb = shift;

    return !($fieldb <=($fielda/100)*20);
}

也许?

【讨论】:

  • 我最初尝试使用 fielda 作为 10 和 fieldb 作为 2,它返回 0 就像它应该的那样,但是如果我将数字更改为其他任何东西,它们仍然返回 0,如果它不应该返回 1具有字段a 的 20% 的值
  • 为什么在 check_fieldb 子程序中使用模板?
  • fielda 和 fieldb 是在表单中输入的两个字段,因此它们可以是任何内容。检查应该查看字段 b 是否小于或等于字段 a 的 20%,如果是,则转到错误页面。如果不是该字段验证
  • 以上对我不起作用,希望我能更好地解释这个问题,但这是我第一次真正看到 perl
  • 错误信息是由这部分引起的:foreach my $require (@Required){ if($require eq 'fieldb' && !check_fieldb($value_of_fielda, $value_of_fieldb)){ push(@error , $要求); } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多