Client Script for RegEx

I have a Client Script that uses onChange event to replace commas (,) with dots.

It successfully replaces commas when entered into the field, but it removes dots. For example, 1.1 becomes 11.

Does anyone know why this is happening?

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var fte = g_form.getValue('fte');

    if(fte.indexOf(',') > -1){
        var newStr = fte.replace(',','.');

        g_form.setValue('fte', newStr);
    }       
}

Answer №1

To replace a character in a string, you can utilize the split() and join() functions.

function modifyString(control, previousValue, nextValue, loadingState, isTemplate) {
    if (loadingState || nextValue === '') {
        return;
    }

    var updatedStr = form.getValue('fte').split(',').join('.');

    form.setValue('fte', updatedStr);
}

Answer №2

Experience the power of this code snippet in your client-side script:

function updateValue(control, previousValue, currentValue, loading, isTemplate) {
    if (loading || currentValue === '') {
        return;
    }

    form.setValue('total', form.getValue('total').replace(/,/g, '.'));
}

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

Error message appearing repeatedly and submit button functioning only after second click

I am currently working on my CS50 Final Project, where I am in the process of designing a web app using Flask. Specifically, this issue arises in the login/register page where I am attempting to verify if the username is already taken (through jsonify) and ...

Cease the execution of promises as soon as one promise is resolved

Using ES6 promises, I have created a function that iterates over an array of links to search for an image and stops once one is found. In the implementation of this function, the promise with the fastest resolution is executed while others continue to run ...

Ajax requests that are delayed to the same URL with identical data

While waiting for the back end developers to add a "cancel all" function, which would cancel all tasks tracked by the system, I am trying to create a workaround by cancelling each task individually. The cancellation REST service requires an ID in the form ...

Using MSAL for secure authentication and authorization in React.js

I am new to React and currently working on implementing Single Sign On Authentication in my React App. Goals: Create a login page where users can enter their email address When the user clicks sign-in, display the SSO popup (using Azure AD) for acceptin ...

Tips for avoiding a 500 GET error due to an empty request during page loading

I recently encountered an issue with a form containing a dependent drop-down menu that reloads after form submission to preselect the main choice that was made before submission. The problem arises when the page initially loads without any parameters being ...

Creating a JavaScript script to implement a CAPTCHA feature on Google Forms

I'm looking to implement a JavaScript solution that can prevent spam on Google Forms. The idea is as follows: Generate a random number a between 1 and 1000; Generate another random number b between 1 and 1000; Obtain input from the user, storing it a ...

Tips for accessing the elements of an associative array passed from PHP to JavaScript via AJAX in JavaScript

Currently, my task involves retrieving and displaying a multidimensional array from a PHP file using an AJAX function. The structure of the array in the PHP file is as follows: Array ( [0] => Array ( [username] => klara [lev] =& ...

JavaScript code that allows users to select text on a website

As I work on developing a chrome extension, my goal is to allow users to select text. I successfully achieved this by running the code in the chrome console. document.onselectstart=new Function ("return true"); However, when I attempted to incorporate th ...

Is there a way to delete a field from a JSON object using JavaScript?

Searching for a way in Node.js to eliminate the date and operation fields from the database. Any suggestions on how to do this? Currently, all fields are being transferred to the FE. The collection pertains to MongoDB. collection.find({'recordType&ap ...

Utilizing JSON information acquired through AJAX within a distinct function

Sorry if this question has been asked before, I tried following suggestions from another post but they didn't work for me. What I'm trying to do is fetch some JSON data, save a part of it in a variable, and then use that variable in a different f ...

Struggling to pass jasmine test when calling injected service

I've been grappling with unit testing in Angular and have hit a few obstacles along the way. While trying to set up a plnkr for my question regarding a failing Jasmine test that's making an $httpbackend call, I encountered a different error. It w ...

Dividing one SVG into Multiple SVGs

I'm facing a challenge with loading an SVG overlay on a Google Map. The SVG file is quite large, about 50mb, resulting in a delay of around 10 seconds for it to load in the browser. One solution I'm considering is splitting the SVG into 171 smal ...

What is stopping TypeScript from assigning certain properties to the HTMLScriptElement?

I'm currently working with TypeScript and React, and I'm facing an issue with a function that is meant to copy script attributes between two elements: type MutableScriptProperties = Pick< HTMLScriptElement, 'async' | 'crossO ...

Rearranging an array using the numbers contained within the array elements in JavaScript

I'm currently working on unscrambling an array. The array contains string elements that are not in the correct order, with numbers attached to indicate their desired position. My goal is to extract these numbers from each item and rearrange them in a ...

`Can I distribute configuration data to all components using a JavaScript file?`

My approach involves using config data to simultaneously affect the status of all components. I achieve this by importing the config object from the same JavaScript file and incorporating it into each component's data. It appears to work seamlessly, ...

Reduce the amount of ajax calls

Currently, I have implemented checkboxes that serve as search filters on a website. Every time a user checks a filter box, an ajax request is triggered to fetch data from the server and display it. The issue arises when users select multiple checkboxes in ...

Merge a dropdown menu with an alphabetically arranged list that is interactive with clickable options

I am still learning HTML and Javascript but I'm doing my best. Currently, I am facing a challenge where I need to create a button that, when clicked, opens a dropdown menu containing a table of data. The user should then be able to select a number fr ...

deliver a promise with a function's value

I have created a function to convert a file to base64 for displaying the file. ConvertFileToAddress(event): string { let localAddress: any; const reader = new FileReader(); reader.readAsDataURL(event.target['files'][0]); reader ...

The Socket IO server fails to broadcast a message to a designated custom room

I am currently working on developing a lobby system that allows players to invite each other using Socket io version 4. The process involves the client sending a request to create and join a room, followed by emitting messages to other clients in the same ...

Receiving an error message of ".then is not a function" while using Inquirer

I am encountering an issue with a basic function I am trying to run using Node.js and inquirer. When I attempt to execute it, the console log shows me the error message: "TypeError: inquirer.createPromptModule(...).then is not a function." Although I have ...