验证textbox1值是否大于textbox2 [英] Validate textbox1 value cant greater than textbox2

查看:63
本文介绍了验证textbox1值是否大于textbox2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮帮我...



i有2个文本框..



Textbox lastprice = 100

Textbox harga = 101



i想验证文本框harga ...



我的尝试:



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>

推荐答案

row-> id_barang,'价格'); ?>禁用=已禁用>
< / div>

< div class =form-group>
< label> Harga Barang per satuan< / label>
< input type =textname =hargaclass =form-controlonkeypress =return isNumberKey(event)placeholder =输入harga barangautofocus =autofocus required =required/>
< / div>
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>


您的 checkPrice 函数需要返回 true 如果验证通过。



两个本地变量ables将是 string s; 字符串类型没有名为 val 的属性。



我猜你想把这些值比作数字而不是字符串。如果将它们作为字符串进行比较,则2大于10



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".

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;
}


我猜你的代码是正确的,除了if语句中的.val。如果从两个变量harga和最后价格中删除.val,它应该按预期的方式工作。您已经将控件/元素的值赋给变量,因此,您不需要'.val'只需删除它。
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.


这篇关于验证textbox1值是否大于textbox2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆