Utilizing jQuery to convert letter input values into English: a step-by-step guide

I am developing a basic web application using Jquery. In this application, I aim to restrict user input language. For instance, if a user enters the Russian letter б in the input field, I want to automatically convert it to an English character. Below is a simplified example of what I am attempting:

$("#name").keyup(function() {
    var charsObject = {
        'а': 'a',
        'б': 'b',
        'г': 'g',
    };
    console.log($(this).val())
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="name">

Answer №1

$("#name").keyup(function(obj) {
  var charMap = {
    'а': 'a',
    'б': 'b',
    'г': 'g',
  };
  if (charMap[obj.key]) {
    console.log(charMap[obj.key])
  } else {
    console.log(obj.key)
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="name">

Answer №2

Here is a method to sanitize input in real-time, whether you are typing or pasting text using the right mouse button:

$("#name").on("input", function() {
  var carr='бb,гg'.split(',').map(e=>e.split(''));
  let s=$(this).val();
  carr.forEach(([k,v])=>s=s.replaceAll(k,v));
  $(this).val(s)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="name">

In this updated version, I have replaced the charsObject with an array of arrays (carr) for a more direct processing in the input event handler.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

Asynchronous JavaScript function within a loop fails to refresh the document object model (DOM) despite

I have been working on a function that utilizes ajax to retrieve instructions from a backend server while the page is loading. The ajax code I've written retrieves the instructions based on the number provided and displays them using the response.setT ...

Disabling the scrollbar within angular elements

Trying to remove the two scrollbars from this code, but so far unsuccessful. Attempted using overflow:hidden without success filet.component.html <mat-drawer-container class="example-container" autosize> <button type="button&qu ...

Incorrect rendering of the <li> tag

I've been working on creating a simple web "to do list" but I've encountered an issue. When I manually add todos and click on them, the 'text-decoration: line-through;' property is ignored, and I can't see the strikethrough effect ...

JavaScript Auto-Closing Modal Pop-Up in HTML

I am looking to implement a feature similar to what is seen on Banks or American Express websites, where a MODAL Popup notifies users that their session is about to expire. In addition to this, I would like to include an "Auto-Close" Popup event that will ...

Custom navigation bar created using Bootstrap 4

My webpage was created using Bootstrap 4, but I am having an issue with the navbar toggler not aligning properly within the navbar on mobile view. You can see the problem in the screenshot below: https://i.sstatic.net/aBejI.png If you visit the url for t ...

Ways to position cursor at the end in the Froala Editor

My current dilemma involves the need to position the focus/cursor at the end of text that is loaded within the Froala editor. I attempted to accomplish this using the focus event of Froala (events.focus), but unfortunately, it did not yield the desired out ...

The form submission messages that are being received are appearing blank, even when the name field is populated with an

Hey there! I'm experiencing an issue with my form. Even though I've set the name attribute to "" for all fields, the submitted data comes back blank in my email. Here's a snippet of my code: <div class="col-lg-6 ...

Issues with the Textarea StartsWith Function Being Unresponsive

Attempting to use the startsWith function on a textarea, but it seems like there may be an error in my code. Since I am not well-versed in Javascript, please forgive any obvious mistakes. I did try to implement what made sense to me... View the Fiddle here ...

Using jQuery setTimeout within a forEach loop

Currently, I am fetching an array of messages using 'getJSON method. My intention is to display each message for 3 seconds before moving on to the next one. The process involves loading an HTML file and applying a CSS class to each message. However, m ...

What could be causing the error message 'Uncaught SyntaxError: Identifier 'randFilmIndex' has already been declared'?

function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); } function arrayRemove(arr, value) { return arr.filter(function(ele){ return ele != valu ...

Enhance your online shopping experience with a personalized touch by incorporating an Ajax add to cart button on

Currently, I am working on customizing the add to cart button on a single product page. After implementing an ajax call solution from LoicTheAztec's post here, I have successfully added the feature. The code works perfectly fine, but now I am faced w ...

Transmitting PHP variable to JavaScript using Callback Method

I am looking to implement password validation using JavaScript along with an Ajax function. Upon successful validation, I aim to return a boolean variable (true or false) and perform specific actions in my PHP file based on the callback. Unfortunately, my ...

Experience the thrill of receiving a triumphant notification upon submission of your form

I'm currently learning about jquery and how to use it with ajax to insert and display data in a mysql table. I've been trying out some code that inserts and displays records from a mysql database. Right now, my goal is to make the success message ...

Troubleshooting problems with anchor-targets in Skrollr

I am experimenting with having divs overlap each other as the page is scrolled using Skrollr. Initially, I was able to achieve the desired effect with two divs. However, when trying to incorporate a third div, only the first and last ones seem to work prop ...

The horizontal scrolling feature will not activate

I meticulously followed the instructions in a YouTube tutorial to implement this code. What's puzzling is that while the code works perfectly for the person in the video, allowing the grid items to scroll to the right, it doesn't have the same ef ...

Equal vertical alignment in the center using flexbox

My flexbox layout is set up as shown in the code snippet below: html { height:100%; width:100%; } body { display:flex; flex-direction:column; align-items:stretch; height:100%; width:100%; margin:0; background-color:grey; } header { b ...

Model-view-controller: Error 404 encountered while making an ajax request

Displaying multiple flights in a view with individual buttons for each flight to show details on click. foreach (var item in Model) { <div class="toFrom"> <h3 id=&qu ...

Could you please explain the output of the offset function in jQuery?

After reviewing the code, I am struggling to comprehend the output of the offset function: $(document).ready(function() { $('#goto-show-form').click(function() { $('html, body').animate({scrollTop: $("#show-form").off ...

execute numerous Jssor Caption instances simultaneously

I have a Jssor slider and I want to create an animation for the first slide. My goal is to make two images come from the sides and merge in the center. I tried using a div for each image with a caption, and it works well. However, the second image starts ...