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 525769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:42:23+00:00 2026-05-13T08:42:23+00:00

I need to only have 1 character at any time in an input field.

  • 0

I need to only have 1 character at any time in an input field. The only way I can think of doing this is by having javascript keep everything in the box always selected and making the maximum input length 1 so that when I type something new, whatever was in there gets overwritten (since it is selected). That way I don’t have to use backspace or anything. So far I have only gotten as far as getting everything to select when you click in the box. I have this:

input type="text" id="txt1" size="30" maxlength="1" onkeyup="showHint(this.value)" onFocus="this.select()" onBlur="this.select()" onClick="SelectAll('txt1');" value="Click here, then press a key"/>

I have a couple different things going on in there to try to get it all selected (like the onFocus and the onBlur which don’t work). Any idea how I could keep everything in that box permanently selected? Or is there a better way altogether to keep a single character in the box?

  • 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-13T08:42:24+00:00Added an answer on May 13, 2026 at 8:42 am

    How about this algorithm:

    onfocus : select all content
    onkeydown : delete all content
    onkeyup : select all content

    If you need the content to always be valid (i.e., you need to have one valid character in the box) you can do some extra validation on keyup and blur. If the value is valid save the valid value somewhere. If the value is not valid restore the previous valid value.

    This would handle the situation where the key being pressed was something like the “tab” or “enter” buttons.

    This logic may not work depending on what other actions you are taking on key presses, if any. But given the parameters in your question, this sounds like this will work.

    Proof of concept code. Modify to your problem and tastes.

        <input id="test" onkeydown="deleteAllContent();" onfocus="selectAllContent();"
        onkeyup="validate();selectAllContent();" onblur="validate();" maxLength="1">
        <script>
    var lastValid = "B";
    function deleteAllContent() {
        document.getElementById('test').value = '';
    }
    
    function selectAllContent() {
        document.getElementById('test').select();
    }
    
    function validate() {
        var value = document.getElementById('test').value;
    
        if (value.length == 0) { // or whatever makes the value invalid
            document.getElementById('test').value = lastValid;
        } else {
            lastValid = value;
        }
    }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have only one button in showConfirmDialog . I tried this: int
I only have access to 'C' and need to replace characters within a character
I only have one computer (running OS X), and need to test in both
I have a large project but I need only some folders in my svn
I need to choose an ORM for a project and I only have some
Assume that I only have a country code (en, de, fr) and I need
I need a captcha solution for GAE in Java. My clients only have access
I need to only get tumblr posts which have been tagged with 'diary' Here
I have a class called Property that I only need to display a few
I have 1000 rows of data but need to only display 100 rows at

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.