How do you automatically set text box to Uppercase?

后端 未结 12 764
爱一瞬间的悲伤
爱一瞬间的悲伤 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:38

    The answers with the text-transformation:uppercase styling will not send uppercased data to the server on submit - what you might expect. You can do something like this instead:

    For your input HTML use onkeydown:

    
    

    In your JavaScript:

    function upperCaseF(a){
        setTimeout(function(){
            a.value = a.value.toUpperCase();
        }, 1);
    }
    

    With upperCaseF() function on every key press down, the value of the input is going to turn into its uppercase form.

    I also added a 1ms delay so that the function code block triggers after the keydown event occured.

    UPDATE

    Per remommendation from Dinei, you can use oninput event instead of onkeydown and get rid of setTimeout.

    For your input HTML use oninput:

    
    

提交回复
热议问题