user3499262
user3499262

Reputation: 39

validate jquery setting max and min for validate

I had a question about val() using javascript and I wanted to find out if anybody could tell me how to set a min max parameter for val(). Basically I want to check if my inputs age is 1 but smaller than 3. Here is the code:

 if ($.trim($("#age").val()) > 1 < 3) { 
     // Code goes here
 }

I know the code above is not working. How can I set that validity max and min parameters?

Upvotes: 0

Views: 52

Answers (1)

Jeremy
Jeremy

Reputation: 1972

This will work for ages 1-3 also allowing for age 1 and 3

var _age = $.trim($("#age").val());
if ( _age >= 1 && _age <= 3) {
    // Code goes here
}

You may have issues since age is currently a string. You can force it to an integer by doing this.

    var _age = $.trim($("#age").val());
    _age = parseInt(_age);
    if ( _age >= 1 && _age <= 3) {
        // Code goes here
    }

The last should work just fine

Upvotes: 1

Related Questions