douzhao6584 2014-04-30 06:18
浏览 29
已采纳

如何在php中为html5表单数字输入验证编写正则表达式模式?

I have a form in which there are many form fields which are dynamically being generated and I am trying to limit a numeric input field to a minimum and maximum value via the following code which isn't working in firefox

    $products=getAllProducts();//an array of product objects
    echo '<form method="POST" action="">';
    for($i=0;$i<(count($products));$i++){
        echo '<label>'.$products[$i].getName().'</label>';
        echo '<input type="number" pattern="\d*" min="1" max="'.$products[$i].getAvailableQty().'" name="'.$products[$i].getId().'"></input>';

    }
    echo '<input id="cartButton" type="submit" value="Submit" formaction="cart.php" />';
    echo '</form>';

Some one suggested me to use regular expressions but I don't know how to write a regular expression each time for a different a min value as 1 and max value as $products[$i].getAvailableQty() value.Any help is appreciated

  • 写回答

1条回答 默认 最新

  • dongwen9051 2014-04-30 08:34
    关注

    You can use the following function for getting the regular expression

    function getInputValidationRegex($qty){
        $qtyCopy=$qty;
        $regexpArr=array();
        $noOfDigits=strlen((String)$qty);
    
        for($i=1;$i<=$noOfDigits;$i++){
            if($i<$noOfDigits){
                $str='';
    
                if($i==1)
                    $str.='[1-9]';
                else
                    $str.='[0-9]{1,'.($i-1).'}';
    
    
            $regexpArr[]=$str;
            }
            if($i==$noOfDigits){
                $str='';
                for($k=1;$k<=$i;$k++){
                    $lastDigit=$qtyCopy%10;
                    $qtyCopy=floor($qtyCopy/10);
                    if($k==$i){
                        $str2='[1-'.$lastDigit.']'.$str;
                        $str=$str2;
                    }
                    else{
                        $str2='[0-'.$lastDigit.']'.$str;
                        $str=$str2;
                    }
    
                }
                $regexpArr[]=$str;
            }
    
        }
        return implode("|",$regexpArr);
    
    }
    

    and modify your code as follows

     $products=getAllProducts();//an array of product objects
        echo '<form method="POST" action="">';
        for($i=0;$i<(count($products));$i++){
            echo '<label>'.$products[$i].getName().'</label>';
            echo '<input type="number" pattern="'.getInputValidationRegex($products[$i].getAvailableQty()).'" name="'.$products[$i].getId().'"></input>';
    
        }
        echo '<input id="cartButton" type="submit" value="Submit" formaction="cart.php" />';
        echo '</form>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀