简体   繁体   English

检查一个或两个输入字段是否为空,值为 0 或负数

[英]Check if one or both input fields are empty, have 0 as value or a negative number

I have two input fields that look like this:我有两个如下所示的输入字段:

<input type="number" class="formateninput form-control hoogtebreedte" name="breedte" max="<?php echo $explodemax[0]?>" min="<?php echo $explodemin[0]?>">
<input type="number" class="formateninput form-control hoogtebreedte" name="hoogte" max="<?php echo $explodemax[1]?>" min="<?php echo $explodemin[1]?>">

I don't want for people to be able to enter a value of 0, less then 1 or nothing before continuing.我不希望人们在继续之前能够输入 0、小于 1 或什么都不输入。

So in my jquery I have the following if statement:因此,在我的 jquery 中,我有以下 if 语句:

if($('.hoogtebreedte').val() !== '' && $('.hoogtebreedte').val() !== 0 && $('.hoogtebreedte').val() > 0){
go ahead
}else{
stop
}

Everything works except when you add a number, for example 90 to the first input, and 0 to the second, it still proceeds.一切正常,除了当你添加一个数字时,例如 90 到第一个输入,0 到第二个,它仍然继续。 Why is that, how can I fix it?为什么会这样,我该如何解决?

You were checking only first value of .hoogtebreedte class so the second value was not check.您只检查.hoogtebreedte class 的第一个值,因此未检查第二个值。

Below is working example:下面是工作示例:

 function test () { if($('.hoogtebreedte').eq(0).val().== '' && $('.hoogtebreedte').eq(0).val() > 0 && $('.hoogtebreedte').eq(1).val().== '' && $('.hoogtebreedte').eq(1).val() > 0){ console.log('everything is ok') } else { console.log('something is wrong') } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="number" class="formateninput form-control hoogtebreedte" name="breedte" max="99" min="0"> <input type="number" class="formateninput form-control hoogtebreedte" name="hoogte" max="99" min="0"> <button onclick='test()'>test</button>

In addition, for multiple class you can use this code:此外,对于多个 class 您可以使用以下代码:

 function validate () { const validate = Array.from($('.hoogtebreedte')).every((_,index) => $('.hoogtebreedte').eq(index).val().== '' && $('.hoogtebreedte').eq(index)?val() > 0) return validate. console:log('everything is ok'). console.log('something is wrong') }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="number" class="formateninput form-control hoogtebreedte" name="breedte" max="99" min="0"> <input type="number" class="formateninput form-control hoogtebreedte" name="hoogte" max="99" min="0"> <input type="number" class="formateninput form-control hoogtebreedte" name="hoogte" max="99" min="0"> <button onclick='validate()'>validate</button>

You have to parse the value of your input as an integer before doing your validations, since the value of an input element is always a string.在进行验证之前,您必须将输入的值解析为 integer,因为输入元素的值始终是字符串。

Working example:工作示例:

 const check = () => { let a = parseInt(document.getElementById("a").value) let b = parseInt(document.getElementById("b").value) if(a >= 1 && b >= 2) { alert("Everything is good.") } else { alert("Either 'a' or 'b' are less than 1 or empty.") } }
 <input type="number" id="a"> <input type="number" id="b"> <button id="check" onclick="check()">Check</button>

Note: I'm not checking for value equal to the empty string, since parseFloat will return NaN (Not a Number) for a value that can't be parsed as an integer and NaN >= 1 is always false.注意:我没有检查等于空字符串的值,因为 parseFloat 将为无法解析为 integer 的值返回 NaN(非数字),并且 NaN >= 1 始终为 false。

<input type="number" id="breedte" class="formateninput form-control hoogtebreedte" name="breedte">
<input type="number" id="hoogte" class="formateninput form-control hoogtebreedte" name="hoogte">
<input id="btnSubmit" type="button" value="Submit" />

$(document).ready(function () {
    $('#btnSubmit').click(function () {
        var input1 = parseInt($('#breedte').val());
        var input2 = parseInt($('#hoogte').val());
        if (input1 > 0 && input2 > 0) {
            alert('Go ahead');
        } else {
            alert('Stop!!!');
        }
   });
});

This is happing just because you are taking value from class in jQuery it treat as an array.这很高兴,因为您从class中的 class 中获取价值,它被视为一个数组。

so you in logically you are second input is not getting validate.所以你在逻辑上你是第二个输入没有得到验证。

each will solve your problem. each将解决您的问题。

$('.validate').on('click',function(){
    var status = false;
    $('.hoogtebreedte').each(function(){
        var t = $(this);
        if(t.val() === '' || t.val() == 0 || t.val() < 0){
            status = true
        }
    })
    if(status) {
        console.log('stop')
    } else {
        console.log('go')
    }

})

Working Code here工作代码在这里

Add on('input') event in order to check the condition every time you introduce a number and make the function 'generic' to work on any input.添加on('input')事件,以便在每次引入数字时检查条件并使 function '通用'在任何输入上工作。 If the inserted value is 0 or 1, make input red如果插入的值为 0 或 1,则使输入变为红色

 function validateInput(myInput) { $('#' + myInput).on('input', function(evt) { var value = evt.target.value console.log(value) if (value === '0' || value === '1') { evt.target.className = 'invalid' } else { evt.target.className = 'valid' } }) } validateInput('first') validateInput('second')
 input.valid { border: 1px solid green; } input.invalid { border: 1px solid red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="number" id="first" class="formateninput form-control hoogtebreedte" name="breedte" max="<?php echo $explodemax[0]?>" min="<?php echo $explodemin[0]?>"> <input type="number" id="second" class="formateninput form-control hoogtebreedte" name="hoogte" max="<?php echo $explodemax[1]?>" min="<?php echo $explodemin[1]?>">

$('.hoogtebreedte').val() !== 0 && $('.hoogtebreedte').val() > 0

are not working correctly because.val() returns a string and not a number.无法正常工作,因为 .val() 返回的是字符串而不是数字。

therefore, these two tests are always true因此,这两个测试总是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM