1

Hei

Have anyone idea of input mask in jquery For example a field must have three letters followed by 4 numbers. cda1234. If one writes something in the field that does not fit with this must be removed continuously.

4 Answers 4

3

Did you check the Masked Input Plugin for jQuery

Sign up to request clarification or add additional context in comments.

Comments

1

Cooked an fast example up, see: http://jsfiddle.net/g8KSg/2/

Checks for non integers for the first 3 characters, and then only accept integers.

Could proberbly be made wiser with regex, but this should give a starting point :)

$('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);
        }
    }
});

9 Comments

which reference of jquery i need to add in header tag ?
What do you mean? Add the jquery lib to you header, and the script inside $(document).ready(function() { // put all your jQuery goodness in here. });
if i want this function onBlur how to do that ?? instead of keyup
just replace keyup with blur - but it will only "check" your input when you leave the input field then :)
ya that i want . iam trying this but its not validating when i leave text box, iam calling like this onblur="CheckInputMask(this)". and i have puted above code iside function CheckInputMask() { here above code} but it seems not working when i leave the textbox
|
0

Hi there these links should give you the building blogs on how to achieve this.

First, select the first 3 charectors, here is how: http://www.w3schools.com/jsref/jsref_substring.asp

then check if they are numbers: checking if number entered is a digit in jquery

And so on...

1 Comment

I was recently admonished to avoid w3schools at all costs, as the website's incorrectness is legendary according to w3fools.com. I thought I'd pass it along. The link you use may be thoroughly adequate, however.
0

Its very easy to implement masks with mixed digit types on jQuery Mask Plugin (github.com/igorescobar/jQuery-Mask-Plugin)

Your will look like this: $('.your-field').mask('SSS0000')

And its all set. The plugin will take care of invalid input and everything.

Cheers, Igor.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.