Simple question, please help

Discussion in 'JavaScript' started by Mackos, Aug 9, 2010.

  1. #1
    Hi!
    I have one form with input type=text
    but I want to put there only amounts that are bigger or equal $minimalamount
    if the amount will be smaller i want to show warning, and do not process form.
    How can I do it ?
     
    Mackos, Aug 9, 2010 IP
  2. wab

    wab Member

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    if $minimalamount comes from your database or is created with some php, it might be better to do your test with php and create your html code from there with something like:
    
    <?php
    if($minimalamount>=1000){
        $val = $minimalamount;
    }else{
        $val = "WARNING";
    }
    ?>
    <input type="text" name="myfieldname" id="myfieldid" value="<? echo $val; ?>">
    
    PHP:
    You should use Javascript only if you have an interaction between the user and the form.
    you can call your action with the "onBlur" trigger, or use the jquery function .blur() on your field, which will give you something like:

    
     <script type="text/javascript">
            $(document).ready(function () {
                $('#myfieldid').blur(function(){
                    if($(this).val()<1000){
                       //do your action here:
                       $(this).val('WARNING');
                    }
                });
            });
     </script>  
    
    Code (markup):
     
    wab, Aug 9, 2010 IP
  3. Mackos

    Mackos Well-Known Member

    Messages:
    364
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    It didn't work'ed :(
    My input look just like this:
    And I did like you sayed:

    <script type="text/javascript">
    $(document).ready(function () {
    $('amount').blur(function(){
    if($(this).val()<10){
    //do your action here:
    $(this).val('WARNING');
    }
    });
    });
    </script>
     
    Mackos, Aug 9, 2010 IP
  4. wab

    wab Member

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    well <? $kwota ?> is php, so you should definitely use php and not javascript. The blur() function needs the user to edit the field to be triggered. If you really want to use jquery, don't forget to call the library.
    In php, if you replace
    <? $kwota ?>
    PHP:
    by
    
    <? if($kwota >=10){ echo $kwota ; }else{ echo "WARNING";} ?>
    
    PHP:
    it should work
     
    wab, Aug 9, 2010 IP
  5. Mackos

    Mackos Well-Known Member

    Messages:
    364
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Nope, It redirected me into site where user shoud put number higher or equal 10 ...
    I want it just to return into site with form, or show error and do not redirect anywhere.
     
    Mackos, Aug 9, 2010 IP
  6. wab

    wab Member

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #6
    can you put all the code around your input because I'm sorry but I don't get your problem...
     
    wab, Aug 9, 2010 IP