Javascript form validation inside PHP

I have a simple problem, but no matter what I try, I do not see to make it work. I have a form on a php page, and I need to check the qty value in my form so that it does not exceed $ qty (the value is derived from mySQL) and not less than zero. It sounds simple - hmm, I would like to ... I had this, checking if the value was numerical, and in my attempts to do this work I even broke it - not a good morning ... lol!

Here is a tip from my JavaScript Fn:

<script type='text/javascript'>
    function checkQty(elem){
        var numericExpression = /^[0-9]+$/;
        if(elem.value.match(numericExpression)){
                return true;
            }else{
                alert("Quantity for RMA must be greater than zero and cannot be more than the original order!");
                elem.focus();
                return false;
        }
    }
    </script>

The function is called from the submit button, onClick:

<input type="submit" name="submit" onclick="checkQty(document.getElementById('qty')";">  

I tried:

var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression) || elem.value < 0 || elem.value > <? int($qty) ?>){

No dice .... HELP!?!

+3
source share
3 answers

, , $qty.

, or (||) an (& &) if- if, .

.

.

var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression) && elem.value < 0 && elem.value > <? echo $qty ?>){
+1

?

  <input type="submit" name="submit" onclick="return checkQty(document.getElementById('qty'));"> 
0

You just need to make the PHP value available to javascript, for example.

 <script type='text/javascript'>
 function checkQty(elem, max_value){
    if(parseInt(elem.value)>0
       && (parseInt(elem.value)<=max_value)){
            return true;
    }else{
            alert("Quantity for RMA must be greater than zero and less than original order!");
            elem.focus();
            return false;
    }
 }
 </script>
 ...
 <?php
     $max_value=(integer)method_of_fetching_max_value();
     print "<input type='submit' name='submit' 
             onclick='checkQty(document.getElementById(\"qty\", $max_value))'>  
     ";
 ?>

WITH.

0
source

Source: https://habr.com/ru/post/1738661/


All Articles