TheSmile
TheSmile

Reputation: 360

jQuery validate numeric textbox and given min & max value

I wish to given my validation with min and max number and then giving a correct alert message. How to set min and max into the script below?

function isvalidated(){
    //CURN
    if(!$.isNumeric($('#txcurn').val())){
        alert("Please enter numbers");
        return false;   
    } 
}

Upvotes: 0

Views: 8127

Answers (1)

john Smith
john Smith

Reputation: 17906

how about using ternary operator :

function isvalidated(input,min,max){

    if(!$.isNumeric(input)){
        alert("Please enter numbers");
        return false;   
    } else {
        message = (input <= max) ? (input >= min ? "input is valid" : "input must be greater than "+min  ) : "input must be smaller than "+max; 
        alert(message);
    }
}

call it like :

isvalidated($('#txcurn').val(),1,10)

Upvotes: 1

Related Questions