ana
ana

Reputation: 47

jQuery text box user's input length

I have some jQuery code where I want to find user's input length but every time instead of giving user's input length, it is returning max length defined on the text box. How can I achieve this?

What I want to achieve using jQuery is this: I want whenever user type 0, I want to convert it to 000, but when user type 70, it is also converting it to 000, which I want to keep as it is. So I want this to fire only when user input length is less then 1. Input user box has a max length defined in HTML which is 3.

Your input is really appreciated.

Here is my jQuery code:

$(document).ready(function(){
    $("input:[id*=txtFairIsaacScore]").keydown(function(event){
        if($("input:[id*=chkFairIsaacScoreEdited]").is(":checked")){
            //$("input:[id*=txtFairIsaacScore]").unmask();

            var cp_value =  $("input:id*=txtFairIsaacScore]").val().length;

            alert(cp_value);

            if(cp_value <= 1){
                if ((event.keyCode == 48) || (event.keyCode == 96)){
                    $("input:[id*=txtFairIsaacScore]").val("000");
                }
             }
        }
    });
});

Upvotes: 0

Views: 13635

Answers (2)

Jayendra
Jayendra

Reputation: 52779

Not sure for the length part ...

you can check -

<input type='text' id='test' maxlength='3'/>

$('#test').bind('keyup',function(){
    if($(this).val().length < 1)
        $('#test').val('000');

})

http://jsfiddle.net/nrVGL/7/

Upvotes: 0

Sedat Başar
Sedat Başar

Reputation: 3798

<input type='text' id='inp'/>

$('#inp').live('keyup',function(){
    alert($(this).val().length)
})

this works fine for me u can check here http://jsfiddle.net/nrVGL/1/

Upvotes: 4

Related Questions