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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试