0

我想验证我的表单以检查特定值的字段。如果用户在文本框中键入大于 5 ( >= 5) 的值,则验证应返回错误。如何使用 jQuery 验证来做到这一点?

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

3 回答 3

1

you can try something like this:

you might want to change your value='' to 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.

于 2013-04-30T07:51:03.983 回答
0

如果你想使用更多..使用类并循环它们

<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>
于 2013-04-30T08:23:20.353 回答
0

<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>
于 2013-04-30T07:55:05.740 回答