


How to force the text of the input box to be displayed in uppercase? (code example)
The content of this article is to introduce how to force the text of the input box to be displayed in uppercase? (Code sample), let everyone master multiple methods of forced conversion to uppercase. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Sometimes, in a form, you only want to accept uppercase text in certain input or textarea fields. So how do I force input lowercase letters to be converted to uppercase text? From JavaScript to CSS and server-side code, there are actually many ways to solve this annoying problem.
Let’s take a look at the implementation method through a simple code example.
1. Use JavaScript to change text when inputting (must use English input)
Use toUpperCase() to convert uppercase
This is the most obvious and common method, but also the least user-friendly. On every keystroke, a JavaScript event is fired to convert the value to uppercase.
<input type="text" onkeyup="this.value = this.value.toUpperCase();">
Let’s take a look at the effect. Enter a lowercase ‘a’ and it will appear:
But this method will cause other problems :
1. When we use the Chinese input method and press shift to input characters, repeated characters will appear; therefore, English input must be used.
2. Try entering "AAA" in the input box above, select the "A" in the middle and try to change it to "ABBA". What you end up with is 'ABAB' because setting the value moves the cursor to the end of the text.
Next we improve it, we canlocate the cursor position
If you move the cursor to an earlier position in the text and type, it will be echoed to have been entered the end of the text. So we need some extra code to preserve the cursor position:
<input type="text" onkeyup=" var start = this.selectionStart; var end = this.selectionEnd; this.value = this.value.toUpperCase(); this.setSelectionRange(start, end); ">
To see the effect, enter ss:
and when we enter "AAA", select The "A" in the middle and try to change it to "ABBA", the result of 'ABAB' will no longer appear.
2. Use css php to change text
Actually, it doesn’t matter if the form input is uppercase or lowercase. It is important that:
1. The text seen by the user will be used;
2. Our form handler converts the text to uppercase before use.
With this in mind, we can ditch all the preceding JavaScript and instead use a simple CSS and PHP (or equivalent backend) solution:
html css code
<input style="text-transform: uppercase;" type="text" name="fieldname">
php code:
<?PHP $_POST['fieldname'] = strtoupper($_POST['fieldname']); ?>
Rendering:
Do you see how easy it is if you use the proper techniques! A single CSS style (can be moved to an external stylesheet), plus a line of PHP, may already exist. No JavaScript or jQuery required.
Summary: The above is all the content introduced in this article, I hope it will be helpful to everyone's study.
The above is the detailed content of How to force the text of the input box to be displayed in uppercase? (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
