How can I keep the cursor in place while editing a phone number field on Sencha ExtJS?

After one backspace move, the cursor on the phone number field automatically moves to the end which can be inconvenient if the user only wants to edit the area code. Unfortunately, I am unable to post images at the moment due to insufficient reputation.

Below is the code for the text field button, and if more context is required, please let me know:

{
    name: 'ContPhone',
    itemId: 'ContPhone',
    fieldLabel: I18N.Messages.STR_PHONE,
    maxLength: 25,
    width: 370,
    labelCls: 'formFieldLabel',
    enforceMaxLength: true,
    plugins: Ext.create('Silver.ux.plugin.FormatPhoneNumber'),
    vtype: 'phone'
},

I suspect that the culprit may be the onChange() function in the plugin/extension information provided below. I would appreciate guidance on how to resolve this issue so that the cursor position remains intact even after deleting a digit from the field before saving.

Ext.define('Silver.ux.plugin.FormatPhoneNumber', {
    extend: 'Ext.form.TextField',

    init: function (c) {
        // Phone number formatting
        Ext.apply(Ext.util.Format, {
            phoneNumber: function (value) {
                var phoneNumber = value.replace(/\./g, '').replace(/-/g, '').replace(/[^0-9]/g, '');
                // Formatting for US phone numbers
                if (phoneNumber !== '' && phoneNumber.length === 10 && UI_CULTURE === 'en-US') {
                    return '(' + phoneNumber.substr(0, 3) + ') ' + phoneNumber.substr(3, 3) + '-' + phoneNumber.substr(6, 4);
                } else {
                    return value;
                }
            }
        });

        // Validating phone numbers
        Ext.apply(Ext.form.VTypes, {
            'phoneText': (UI_CULTURE === 'en-US' ?
                'Not a valid phone number. Must be in the format (555) 555-5555.' :
                'Not a valid phone number. Must be a number 0-9.'),
            'phoneMask': /[\-\+0-9\(\)\s\.Ext]/,
            'phoneRe': (UI_CULTURE === 'en-US' ?
                /^(\({1}[0-9]{3}\){1}\s{1})([0-9]{3}[\-]{1}[0-9]{4})$|^(((\+44)? ?(\(0\))? ?)|(0))( ?[0-9]{3,4}){3}$|^Ext\. [0-9]+$/ :
                /^[0-9\s]+$/),
            'phone': function (v) {
                return this.phoneRe.test(v);
            }
        });

        c.on('change', 'onChange', this);
    },

    onChange: function (c) {
        c.setValue(Ext.util.Format.phoneNumber(c.getValue()));
    }
}); 

Answer №1

After some investigation, I managed to solve the issue. It turned out that the problem was resolved by removing the following code snippet:

onChange:  function (c) {
    c.setValue(Ext.util.Format.phoneNumber(c.getValue()));
}

Despite this change, the field continues to verify the number against the specified format criteria. It is necessary for the number to match the criteria in order to save successfully. :)

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

What could be causing the TypeError that is preventing my code from running smoothly?

I can't seem to figure out why I'm constantly encountering the error message Cannot destructure property 'id' of 'this.props.customer' as it is undefined.. Despite double-checking my code and making sure everything looks corre ...

Prevent deletion of already uploaded images in Blueimp by disabling the delete button

After using the blueimp upload script to upload files, I noticed that the file information is saved in a data table along with the upload timestamp. However, when I go to edit the form, the files reappear. The simple task I want to achieve is disabling th ...

Update the style of the legend from bars to a line chart in chart.js version 2.4.0

https://i.sstatic.net/BXcXj.pngI'm currently using chart.js version 2.4.0 for displaying graphs, and I'm having trouble changing the legend style from bar to line. Is there a way to switch the legend display from bar to line in chart.js? Here i ...

Dealing with a JavaScript Problem on Wordpress Using AJAX

Struggling with transitioning a website from Drupal to WordPress, I encountered an issue with a page that utilizes AJAX. A user followed a tutorial on implementing AJAX using JavaScript, PHP, and MySQL. Even though the AJAX functionality works fine on Drup ...

Capture locations from Google Maps

What is the best method to extract all addresses, along with their latitude and longitude, for a specific city (such as Bangalore) from Google Maps using PHP/JavaScript and store it in a MySQL table? I urgently need assistance. Thank You ...

Having issues with unexpected token in Typescript while using "as HTMLElement" type?

I recently started learning Typescript and I encountered an error while using the HTMLElement type in a forEach loop: ERROR in ./app/javascript/mount.jsx Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: /Users/me/projects/m ...

Is there a way to display an animation when a page loads using jQuery that only plays for index.php and not other_page.php?

How can I trigger an animation on page load, specifically for my index.php page and not all other pages on my website? Is there a jQuery function that targets only the index.php page like this: $('index.php').ready(function()); If not, what i ...

Utilizing data compression techniques to minimize network bandwidth consumption

Imagine this scenario: I have a considerable amount of data (greater than KB/MB) that needs to be transferred from an ajax request in JavaScript to a webpage in PHP. Would it be beneficial to compress the data using JS scripting before sending it to the se ...

Display all data using JSONP

I've encountered an issue with JSONP. Although I was able to successfully load my JSONP data into my html file, I am struggling to display all the information. I have attempted using both a for loop and $.each method without any luck. Here is the JSO ...

What is the best method to securely install a private Git repository using Yarn, utilizing access tokens without the need to hardcode them

My initial thought was to utilize .npmrc for v1 or .yarnrc.yml for v2/3/4, but in all scenarios, Yarn does not even attempt to authenticate with Github. nodeLinker: node-modules npmScopes: packagescope: npmAlwaysAuth: true npmAuthToken: my_perso ...

Is there a way to customize the selected option in the autocomplete feature of Material UI components?

Is it possible to customize the CSS of selected options in Material UI autocomplete? Can this be achieved by utilizing the theme? ...

Is there a way to prevent the letters from moving when I hover over them?

What occurs when the numbers are hovered over: https://gyazo.com/20b6426d435551c5ee238241d3f96b4d Whenever I hover over the pagination numbers, they shift to the right, and I am unsure of what mistake I made in my code that's causing this. Below, I ...

Passing a JSON object between pages in Next.js using props during programmatic navigation

In my Next.js project, I have two pages. On the first page, the user fills out a form to create a post. The information is stored in a large JSON object, which needs to be passed to the second page for the user to preview the post before finalizing it. Wi ...

Modifying the HTML <select> element with JavaScript

[resolved] I'm encountering an issue with the code below that is supposed to dynamically change the options in a second drop-down menu based on the selection made in the first menu. I've tried to troubleshoot the problem but haven't been suc ...

Utilizing .env Variables in NestJS Main App Module for Establishing Database Connection

I recently embarked on my first NestJS project, where I initially had a hardcoded database connection string in app.module.ts that worked perfectly fine. However, our project requirements called for fetching the database configuration values from environm ...

React, Storybook - Error TS2307: Button module not found or its type declarations. Can Storybook resolve this issue?

In my React project, I have a Button component created with "create-react-app" that uses absolute paths for importing. When trying to import { Button, ButtonProps } from 'Button', I encountered an error with TS2307. The absolute path 'Butto ...

Customize Link Appearance Depending on Current Section

Looking for a way to dynamically change the color of navigation links based on which section of the page a user is currently viewing? Here's an example setup: <div id="topNav"> <ul> <li><a href="#contact">Contac ...

What is the method for incorporating locales into getStaticPaths in Next.js?

I am currently utilizing Strapi as a CMS and dealing with querying for slugs. My goal is to generate static pages using getStaticPaths and getStaticProps in Next.js. Since I'm working with multiple locales, I have to iterate through the locales to re ...

The issue with Framer motion's whileInView is that it does not animate the element when it is within the viewport in Next.js

In my current Next.js 14 project with the App Router, I decided to play around with animations using Framer Motion. One specific feature I wanted to implement was animating text elements into view as they enter the viewport. The idea is for the text to gra ...

Providing the module with the URL

How can I retrieve the URL within a module? module.exports = function(app, Reviews, Anon, User, url){ var url = url; console.log("url", url)// url is undefined how to get url function postHandler(req, res){ } app.post("/individual/"+ u ...