casper
casper

Reputation: 461

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. How can I do this with jQuery validation?

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

Upvotes: 0

Views: 411

Answers (3)

Yordi
Yordi

Reputation: 195

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>

Upvotes: 0

&#222;aw
&#222;aw

Reputation: 2057

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.

Upvotes: 1

Amit
Amit

Reputation: 15387

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

Upvotes: 0

Related Questions