Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
Help me please...

i have 2 textbox..

Textbox lastprice = 100
Textbox harga = 101

i want validation textbox harga...

What I have tried:

<div class="form-group">
                <label> Harga Barang Terakhir Dibeli per satuan</label>
                <input type="text" name="lastprice" class="form-control" value="<?= __nama('tb_barang', 'id_barang', $row->id_barang, 'harga'); ?>" disabled="disabled">
            </div>

            <div class="form-group">
                <label> Harga Barang per satuan</label>
                <input type="text" name="harga" class="form-control" onkeypress="return isNumberKey(event)" placeholder="Input harga barang" autofocus="autofocus" required="required"/>
            </div>


<script>
function checkPrice(){
        var lastprice = document.getElementById('lastprice').value;
        var harga = document.getElementById('harga').value;
        if(harga.val > lastprice.val){
             alert("entered value is bigger");
             return false;
           }
         }
</script>
Posted
Updated 10-Oct-17 8:17am
Comments
91191109 9-Oct-17 1:49am    
my submit button

 <button type="submit" name="kirim" onclick="return checkPrice()" class="btn btn-success pull-left">Simpan</button> 
Patrice T 9-Oct-17 3:38am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

Your checkPrice function needs to return true if the validation passes.

The two local variables will be strings; the string type does not have a property called val.

I'm guessing you want to compare the values as numbers, not as strings. If you compare them as strings, then "2" is greater than "10".

JavaScript
function checkPrice(){
    var lastprice = parseFloat(document.getElementById('lastprice').value);
    var harga = parseFloat(document.getElementById('harga').value);
    
    if (isNaN(lastprice) || isNaN(harga)) {
        alert("Entered values are not numeric.");
        return false;
    }
    
    if (harga > lastprice) {
        alert("Entered value is bigger");
        return false;
    }
    
    return true;
}
 
Share this answer
 
I guess your code is correct except .val in your if statement. If you remove .val from both variables harga and last price it should work as you expect it to work. You are already assigning value of controls/elements to variables so, you don't need '.val' just remove it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900