3

I have a simple problem but no matter what I try I can't see to get it to work. I have a form on a php page and I need to validate the qty value on my form so that it doesn't exceed $qty (value pulled from mySQL) and is not less than zero. Sounds easy--hmm wish it were..lol! I had it checking if the value was numeric and in my attempts to make this work I even broke that--not a good morning..lol!

Here's a snip of 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've tried:

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

No dice....HELP!?!

5
  • tag changed, although type='text/javascript..lol! Commented Mar 26, 2010 at 13:10
  • Mikey how is this Java related? Why did you roll back the tag change? Is there something we all are not seeing? Commented Mar 26, 2010 at 13:11
  • It's not related--Gordon was offended I originally tagged javascript instead of java..lol! Commented Mar 26, 2010 at 13:18
  • 1
    @Mikey1980 He was not offended m8, its just like big big thing to misplace java with javascript or vice-versa, it would be like talking about car and thinking about carpet. Commented Mar 26, 2010 at 13:22
  • @Mikey1980 I wasn't offended. Java is just not an abbreviation for JavaScript. Java is a programming language very different to JavaScript. You should not say Java when you mean JavaScript. It's just plain wrong. Commented Mar 26, 2010 at 13:47

3 Answers 3

1

Maybe try and view the source of the page and check if the $qty value is be printed out.

Also I think you need to change the or (||) to an and (&&) based on the original if statement, otherwise it will skip the limit checks.

Also echo the variable to print it out.

i.e.

var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression) && elem.value < 0 && elem.value > <? echo $qty ?>){
Sign up to request clarification or add additional context in comments.

5 Comments

also, beware of shortened PHP open tag <? - it doesn't work everywhere.
shouldn't it be actually <?php echo $qty; ?>? I think it should.
I though it worked.. but now if the $qty and form qty are both 1 it throws the alert--I am so confused :s
@Neal - the && doesn't make sense to me, "If form value qty is not numeric OR is less than zero OR is greater-than db's qty then..."
@Mikey - based on the original if statement i.e. if you just added the limit checks into the if statement, it would need to be an and. i.e. if it's a number and greater than zero and less than qty return true, else error...
0

Have you tried?

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

4 Comments

I think you mean: <input type="submit" name="submit" onclick="return checkQty(document.getElementById('qty'));">
@Will Morgan Yes a mistake which I didn't see, fixed now tnx
BINGO--That worked, but any idea how to to compare it to a PHP variable $qty?
@Mikey1980 you're gonna have to ask seperate question for that
0

You just need to make the PHP value available to the javascript e.g.

 <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))'>  
     ";
 ?>

C.

2 Comments

WOW! I love that approach--however my HTML on this page is clean and mainly PHP free--easier for our designer, but will use this for other scripts going forward for sure! KUDOS!!
I tried your method and no dice... if $qty and form qty are both 1 it throws the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.