Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:12:02+00:00 2026-05-23T22:12:02+00:00

I wanted a text field to take only numbers ans some control keys and

  • 0

I wanted a text field to take only numbers ans some control keys and number should be exactly four digit long, not less not more. My validation code is

function checkValidInput()
{
        $(".validateYearTextBox").keydown(function(event)
        {
            // Allow only delete, backspace,left arrow,right arraow and Tab
            if ( 
                   event.keyCode == 46 //delete
                || event.keyCode == 8  //backspace
                || event.keyCode == 37 //leftarow
                || event.keyCode == 39 //rightarrow
                || event.keyCode == 9  //tab
                )
                {
                // 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(); 
                    }   
                }
        });

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

}

Here, my first validation is working, but my send one is not working.

I am calling this validation function in my aspx page like this

<script type="text/javascript">   
    $(document).ready(function(){
        checkValidInput(); 
    }
</script>

What is going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:12:02+00:00Added an answer on May 23, 2026 at 10:12 pm

    Simplify it:

    function checkValidInput() {
        // Allow only delete, backspace, left arrow, right arrow, 
        // Tab, ctrl+v and numbers
        $(".validateYearTextBox").keydown(function(event) {
            if (!((event.keyCode == 46 || 
                event.keyCode == 8  || 
                event.keyCode == 37 || 
                event.keyCode == 39 || 
                event.keyCode == 9) || 
                (event.ctrlKey && event.keyCode == 86) ||  // Edit: Added to allow ctrl+v
                $(this).val().length < 4 &&
                ((event.keyCode >= 48 && event.keyCode <= 57) ||
                (event.keyCode >= 96 && event.keyCode <= 105)))) {
                // Stop the event
                event.preventDefault();
                return false;
            }
        });
        // Edit: Added validate after copy+paste.
        // This removes non-numeric characters and truncates the length
        // to 4 if the user copy + pasted.
        $(".validateYearTextBox").change(function(event) {
            var value =  $(this).val();
            value = value.replace(/[^0-9]/g,'');
            value = value.substr(0,4);
            $(this).val(value);
        });
    }
    
    
    $(document).ready(function() {
        checkValidInput();
    });
    

    http://jsfiddle.net/nwellcome/687kD/

    Edit: Personally I like the Masked Input jQuery plugin but that might be a heavy-handed solution if this is all you need to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to create a control with a TextBox and to bind TextBox.Text property
I've got a date text field I wish to only sometimes attach a DatePicker
I am using the text field delegate method shouldChangeCharactersInRange and I wanted to know
I have encountered strange problem. I have created simple Flash text field control and
if I wanted to insert text from a text field into an alert view,
I wanted to parse a text file that contains unstructured text. I need to
Initially I wanted a checkmark where the text is placed on the left of
What I wanted to do was create a listbox from a delimited text file.
I use emacs for viewing and editing code and other text files. I wanted
What's the best/most efficient way to extract text set between parenthesis? Say I wanted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.