1

I wrote a validation code for an input text field, which will take only numbers and some control keys. I took the help from stackoverclow :), So I am here again to take the help. My validation code is

 $("#txtLevel1Year").keydown(function(event)
            {
                // Allow only backspace,delete,left arrow,right arraow and Tab
                if ( event.keyCode == 46 
                    || event.keyCode == 8 
                    || event.keyCode == 37 
                    || event.keyCode == 39 
                    || event.keyCode == 9)
                    {
                    // let it happen, don't do anything
                    }
                    else {
                        // Ensure that it is a number and stop the keypress
                        if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode <96 ||event.keyCode > 105) ) {
                            event.preventDefault(); 
                        }   
                    }
            });

Now let me tell you what I am looking for, first, I want to restrict this field to take exactly 4 digits.not less not more, the second one is about optimization. The text field(s) where I want this validation to work is(are)

<asp:TextBox runat="server" ID="txtLevel1Year" Width="50px" TabIndex="13"></asp:TextBox>  
                        <asp:TextBox runat="server" ID="txtLevel2Year" Width="50px" TabIndex="17"></asp:TextBox>  
                        <asp:TextBox runat="server" ID="txtLevel3Year" Width="50px" TabIndex="21"></asp:TextBox>  
                        <asp:TextBox runat="server" ID="txtLevel4Year" Width="50px" TabIndex="25"></asp:TextBox>  

Here I can repeat the the validation code 4 times to make this work by grabing four different ids though the validation is exactly same. Can I do anything which can remove this repeatation? If My problem isw not clear to you, please let me know. thanx all of you in advance. This is the edited part of this question I have achieved the goal of optimazation by creatinf a CSS class and call it in the div where I place all the year text boxes. But My limit to make it exactly 4 digits yet to be solved. please help me.

3
  • 1
    With validation like that remember to test the field also with copy&paste - both using keyboard short cut and mouse right click. Commented Jul 22, 2011 at 11:14
  • Would you please let me know how can I achive this? Commented Jul 22, 2011 at 11:16
  • stackoverflow.com/questions/686995/jquery-catch-paste-input Commented Jul 22, 2011 at 11:20

4 Answers 4

2

I assume that your validation works. The trick will be to add a class for all textbox. Say the class name is

.validateTB

Then modify your script as follows

$(".validateTB").keydown(function(event)
        {
            // Allow only backspace,delete,left arrow,right arraow and Tab
            if ( event.keyCode == 46 
                || event.keyCode == 8 
                || event.keyCode == 37 
                || event.keyCode == 39 
                || event.keyCode == 9)
                {
                // let it happen, don't do anything
                }
                else {
                    // Ensure that it is a number and stop the keypress
                    if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode   <96 ||event.keyCode > 105) ) {
                        event.preventDefault(); 
                    }   
                }
        });

Note that only selector is changed in the script.

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

1 Comment

I put all the textboxes in a div, and at the div I called the class and it worked.
1

Remember that you can set <input maxlength="4"> in HTML as an easy solution to the half of the problem. However, this only prevents browser user from entering more than 4 characters, but you can avoid it programatically, by calling some JS function that sets longer value.

Comments

0

Try this: (no blinking, and no copy paste allowed):

$(".validate").keyup(function(event) {
    var val = $(this).val();
    if (val.length > 5) {
        val = val.substring(0, 5);
        $(this).val(val);
        return false;
    }
}).keypress(function(event) {
    if ($(this).val().length > 4) {
        return false;
    }
});

http://jsfiddle.net/xEMzx/

Comments

-1

Ok, first of to have your validation code to all the four fields you can use a class selector (after deining a calss for all your elements):

<asp:TextBox runat="server" class='validate'

 $(".validate").keydown(function(event)

to check for length you could use a keyup event (which in my experience is better for this task)

 $(".validate").keyup(function(event){
         var val = $(this).val();
         if (val.length > 4){
            alert ("Max length is 4");
            val = val.substring(0, val.length - 1);
            $(this).val(val);
            $(this).focus();
            return false;
          }
   });

fiddle here: http://jsfiddle.net/a5BJX/

3 Comments

your last part is not working, I have made he modification like $(".validateYearTextBox").keydown(function(event) { //the same code }); and then pasted your code by replacing the appropiate class name. but its not working
Here is a working fiddle, there were two small errors - copy/paste the code again jsfiddle.net/a5BJX
I am putting a new question, please ans this over there, this part is getting too messi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.