How do you automatically set text box to Uppercase?

后端 未结 12 602
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 23:50

I am using the following style attribute to set the user input to uppercase so that when the user starts typing in the text box for example railway, then it should

相关标签:
12条回答
  • 2021-01-30 00:12

    The issue with CSS Styling is that it's not changing the data, and if you don't want to have a JS function then try...

    <input onkeyup="this.value = this.value.toUpperCase()" />

    on it's own you'll see the field capitalise on keyup, so it might be desirable to combine this with the style='text-transform:uppercase' others have suggested.

    0 讨论(0)
  • 2021-01-30 00:12

    This will both show the input in uppercase and send the input data through post in uppercase.

    HTML

    <input type="text" id="someInput">
    

    JavaScript

    var someInput = document.querySelector('#someInput');
    someInput.addEventListener('input', function () {
        someInput.value = someInput.value.toUpperCase();
    });
    
    0 讨论(0)
  • 2021-01-30 00:13
    <script type="text/javascript">
        function upperCaseF(a){
        setTimeout(function(){
            a.value = a.value.toUpperCase();
        }, 1);
    }
    </script>
    
    <input type="text" required="" name="partno" class="form-control" placeholder="Enter a Part No*" onkeydown="upperCaseF(this)">
    
    0 讨论(0)
  • 2021-01-30 00:14

    You've put the style attribute on the <img> tag, instead of the <input>.

    It is also not a good idea to have the spaces between the attribute name and the value...

    <input type="text" class="normal" 
           name="Name" size="20" maxlength="20" 
           style="text-transform:uppercase" /> 
    <img src="../images/tickmark.gif" border="0" />
    

    Please note this transformation is purely visual, and does not change the text that is sent in POST.

    0 讨论(0)
  • 2021-01-30 00:15

    Set following style to set all textbox to uppercase

    input {text-transform: uppercase;}
    
    0 讨论(0)
  • Using CSS text-transform: uppercase does not change the actual input but only changes its look. If you send the input data to a server it is still going to lowercase or however you entered it. To actually transform the input value you need to add javascript code as below:

    document.querySelector("input").addEventListener("input", function(event) {
      event.target.value = event.target.value.toLocaleUpperCase()
    })
    <input>

    Here I am using toLocaleUpperCase() to convert input value to uppercase. It works fine until you need to edit what you had entered, e.g. if you had entered ABCXYZ and now you try to change it to ABCLMNXYZ, it will become ABCLXYZMN because after every input the cursor jumps to the end.

    To overcome this jumping of the cursor, we have to make following changes in our function:

    document.querySelector("input").addEventListener("input", function(event) {
      var input = event.target;
      var start = input.selectionStart;
      var end = input.selectionEnd;
      input.value = input.value.toLocaleUpperCase();
      input.setSelectionRange(start, end);
    })
    <input>

    Now everything works as expected, but if you have slow PC you may see text jumping from lowercase to uppercase as you type. If this annoys you, this is the time to use CSS, apply input: {text-transform: uppercase;} to CSS file and everything will be fine.

    0 讨论(0)
提交回复
热议问题