meer
meer

Reputation: 225

jQuery input mask

Hey, I am new to jQuery , I want to check onblur on input box this format cda 123 . mean first 3 characters space and 3 integers. Usually I see that code is written on Input id's but if I want on Class then how can I do this . For example I have class="InputMask-cccsnn" I want to have this on this format CheckInputMask(this) on class not on input id . How to change my following code . Please reply

Following is my code on keyup on one input id.

$('input').keyup(function(e){
    var value = $(this).val();
    if(value.length <= 3) {
       char = value.substring(value.length - 1);
        if(!(isNaN(char))) {
            var newval = value.substring(0, value.length - 1);
            $(this).val(newval);
        }
    }
    if(value.length > 3) {
       char = value.substring(value.length - 1);
        if(isNaN(char)) {
            var newval = value.substring(0, value.length - 1);
            $(this).val(newval);
        }
    }
});

Upvotes: 2

Views: 33077

Answers (2)

Ryan
Ryan

Reputation: 3815

You can use try using regular expressions.

if($('input').val().match(/.{3} \d{3}$/)){
        alert('valid');
    }
    else{
        alert('invalid');
    }

Check out a sample demo for your case here

http://jsfiddle.net/ryan_s/WYzhR/1/

Upvotes: 2

Petr
Petr

Reputation: 3249

Just use some jQuery input mask plugin, for example this http://digitalbush.com/projects/masked-input-plugin/

Then you can use:

jQuery(function($){
    $(".InputMask-cccsnn").mask("aaa 999");   
});

Upvotes: 4

Related Questions