close
close
Restricting Input to Single Uppercase Letter in JavaScript

Restricting Input to Single Uppercase Letter in JavaScript

2 min read 09-11-2024
Restricting Input to Single Uppercase Letter in JavaScript

When developing a web application, it may be necessary to restrict user input to a specific format. For instance, if you want to ensure that users enter only a single uppercase letter, JavaScript provides an efficient way to accomplish this. Below is a guide on how to restrict input using JavaScript.

HTML Structure

First, you will need a simple HTML structure with an input field. Here’s an example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Uppercase Letter Input</title>
</head>
<body>
    <form id="letterForm">
        <label for="uppercaseLetter">Enter a Single Uppercase Letter:</label>
        <input type="text" id="uppercaseLetter" maxlength="1" />
        <span id="errorMessage" style="color:red;"></span>
        <input type="submit" value="Submit" />
    </form>

    <script src="script.js"></script>
</body>
</html>

JavaScript Code

Now, let's add the JavaScript code to restrict the input. This code will ensure that the user can only enter a single uppercase letter. You can include this code in a file named script.js.

document.getElementById('letterForm').addEventListener('submit', function(event) {
    event.preventDefault(); // Prevent form submission

    const input = document.getElementById('uppercaseLetter');
    const errorMessage = document.getElementById('errorMessage');
    
    // Regular expression to check for a single uppercase letter
    const regex = /^[A-Z]$/;

    if (regex.test(input.value)) {
        errorMessage.textContent = ""; // Clear any previous error messages
        alert("Valid input: " + input.value); // Alert valid input
    } else {
        errorMessage.textContent = "Please enter a single uppercase letter."; // Show error message
    }
});

Explanation of the Code

1. HTML Structure

  • The input field is limited to one character using maxlength="1".
  • An error message span is included to provide feedback to the user.

2. Event Listener

  • The form submission is handled by an event listener that prevents the default action to ensure validation occurs first.

3. Regular Expression

  • The regex pattern /^[A-Z]$/ is used to validate that the input consists of exactly one uppercase letter.
    • ^ asserts the start of the string.
    • [A-Z] matches any uppercase letter.
    • $ asserts the end of the string.

4. Validation

  • If the input matches the regex, an alert displays the valid input.
  • If not, an error message prompts the user to correct their input.

Conclusion

By following this guide, you can effectively restrict user input to a single uppercase letter in JavaScript. This method not only enhances user experience by providing immediate feedback but also ensures that the data collected meets the specified criteria. Adjust the HTML and JavaScript as necessary to fit your application's design and functionality requirements.

Popular Posts