如何在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

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>';