简体   繁体   English

使用jQuery验证来验证特定值

[英]Validate a specific value with jQuery validation

I want to validate my form to check a field for a specific value. 我想验证表单以检查字段的特定值。 If a user types a value more than 5 ( >= 5 ) into a textbox, validation should return an error. 如果用户在文本框中键入大于5( >= 5 )的值,则验证应返回错误。 How can I do this with jQuery validation? 如何使用jQuery验证来做到这一点?

    <input type='text' name='qty' value='must lower than 5'/>

you can try something like this: 您可以尝试这样的事情:

you might want to change your value='' to placeholder='' 您可能需要将value=''更改为placeholder=''

<input type='text' name='qty' id='txt_qty' placeholder='must be lower than 5'>

here is the script: 这是脚本:

<script type='text/javascript'>
    $(document).ready(function()
    {
        $('#txt_qty').keyup(function()
        {
             var qty = $(this).val();
            if(qty > 5)
            {
                alert('Quantity must not be more than 5');
                $(this).val('');
            }
        });
     });   
</script>

Its just simple but I hope this helps. 它只是简单,但我希望这会有所帮助。

<script type='text/javascript'>
$(document).ready(function()
{
   $("#formID").submit(function(){
        var qty = $('#txt_qty').val();
        if(qty > 5)
        {
           alert('Quantity must not be more than 5');
           $('#txt_qty').val('');
           return false;
        }
        return true;
    });
});
</script>

if you want to use more.. use classes and loop them 如果您想使用更多..使用类并循环它们

<script type='text/javascript'>
$(document).ready(function()
{
   $("#formID").submit(function(){
        var ret= true;
        $(".checkthis").each(function(){
            var qty = $(this).val();
            if(qty > 5){
              alert('Quantity must not be more than 5');
              $(this).val('');
              ret = false;
            }
        });
        return ret;
    });
});
</script>

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

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