Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
1  
Sorry if I am misunderstanding. If you are just trying to make sure a user does not enter a value higher than the available quantity, RegEx is not necessary in this case. Just validate the inputted quantity after the form is submitted, or using javascript. –  Brandon White 20 hours ago
    
I want it to be validated on the client side even when the java-script is turned off! –  user3587824 20 hours ago
    
What does the generated HTML look like? –  Phil 20 hours ago
    
According to developer.mozilla.org/en-US/Firefox/Releases/29 it is supported in recent release. –  Ulugbek Umirov 20 hours ago
    
It can be different each type depending on the $products[$i].getName(),$products[$i].getAvailableQty(),$products[$i].getId‌​()...e.g. <form method="POST" action=""><label>Camera</label><input type="number" pattern="\d*" min="1" max="5" name="p-27"></input> <label>Keyboard</label><input type="number" pattern="\d*" min="1" max="3" name="p-31"></input> <input id="cartButton" type="submit" value="Submit" formaction="cart.php" /> </form> –  user3587824 20 hours ago
show 3 more comments

1 Answer

up vote 0 down vote accepted

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>';
share|improve this answer
    
Thanks a lot it worked ! –  user3587824 11 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.