My goal is to eliminate any characters that are not numbers and any punctuation marks except for the period

I am looking to eliminate all non-numeric symbols and punctuations except for periods ".". I have managed to remove all non-numeric symbols using the following code:

 if (!/^[0-9]+$/.test(this.value)) {
     this.value = this.value.replace(/\D/, "");
}

Can someone guide me on how to achieve this? Thank you for your assistance.

Answer №1

The pattern \D will find any character that is not a digit.

To exclude a dot as well, you can utilize a negative character class like [^\d.]+, which matches any character except a dot or digit.

In this case, since you are replacing it with an empty string, the character class can be repeated 1+ times.

Don't forget to use the global flag /g to replace all instances.

this.value = this.value.replace(/[^\d.]+/g, "");

Answer №2

var newValue = this.value.replace(/[^\d\.]/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

Issue with implementing a custom filter for currency in AngularJS code

I'm tackling a pretty straightforward task here. I just need to integrate a currency filter into the custom filter that I coded. Take a look at the code snippet below: var app = angular.module('testapp', []); app.controller('MainCt ...

How can I add a character at a precise location while keeping the existing tags intact

Latest Update After further testing, it seems that the code performs well with a faux spacer, but runs into issues with the regex. The following scenarios work correctly: Selecting words above or below the a tag Selecting just one line directly above or ...

What is the best way to delete multiple elements from an array using their index values?

When working with an array, the Array.splice method can be used to remove single values. However, if multiple elements need to be removed from an array, such as element 2, 4, and 8 from an array containing 10 elements, using Array.splice(index,1) in a for ...

Angular table elements can trigger a click event that redirects to a unique URL generated from the ID of the selected element

In the angular table, every element should be clickable. When any element in the table is clicked, it should use its ID to perform a new search and redirect to the URL based on that ID of the clicked element. Below is the JSON data from the initial URL. It ...

Attempting to design a universal form field component

While working on a web app with Vue, I found myself in need of creating multiple forms. To make this process easier, I started developing a generic input component that can be reused throughout the application. The plan was to be able to specify the number ...

How can I transfer checkbox data to another page utilizing ajax or javascript?

These are the checkboxes I am using and I want to pass the selected items' values to another page using ajax with javascript. I am looking to send the selected checkbox values through ajax to a different page... Your help would be greatly appreciate ...

The compatibility between Polymer JS and Rails Turbolinks is problematic

Currently, I am facing a challenge in integrating PolymerJs with a Ruby on Rails project that has Turbolinks enabled. The issue arises when I click on a link - the new page loads correctly but my Polymer components do not reinitialize. They appear as if no ...

Implement a delete function within the delete button for every row in DataTables in Angular and incorporate a background color for the button

I am facing an issue where I can add a button to each row in my data tables, but the button is not functional. I am unsure how to implement a delete event for this button. Can anyone provide assistance? It would be great if you could also show me a demo ;) ...

Creating a string pattern in Android is a simple yet effective way to customize the

I am looking to create my own pattern matcher for specific characters. I want to allow only these characters: TN 08 AB 2233. Can anyone help me with creating a pattern for this? I have tried the following pattern, but it erases all characters that I type ...

Updating React state via child components

Encountering a strange issue while working on a project, I managed to replicate it in this jsfiddle. The parent component's state seems to be affected when the child component updates its state. Any insights on what might be causing this? Here is the ...

Tips for maintaining a highlighted navigation link upon selection in Vue.js

My dilemma involves a navigation bar featuring router-links. My goal is to have the selected link visually highlighted so users can easily identify which page they are on. The current setup looks something like this: <div class="mynav"> ...

Selecting radio buttons using Bootstrap and JavaScript

I'm interested in incorporating this bootstrap radio selection feature into my JavaScript code. For example, if option1 is true, I want to execute a specific action... How can I achieve this? <div class="alert alert-info" role="alert"> < ...

Obtain data attributes using JQuery's click event handler

I'm facing an issue with a div structure setup as follows: <div class='bar'> <div class='contents'> <div class='element' data-big='join'>JOIN ME</div> <div class=& ...

Prevent the user from selecting an option if the value is already present

In the process of creating a library membership form, I am utilizing an ajax request to populate the student list in a select option. The goal now is to disable the options for students who are already members of the library. View of the Form <div cla ...

Encountered a problem when attempting to upload files to AWS S3 using React and React AWS S3

One issue I'm facing is receiving a strange response when trying to perform a put operation in my bucket. I am utilizing the react-aws-s3 package which only requires the bucket name, user keys, and region in its configuration. It's puzzling as t ...

When the mouse hovers over the image within the div, it undergo

I want to create "links" that are actually titles of equipment or items I wish to review. When the title is moused over, I want the image to change, but remain changed even after the mouse moves away. Existing code options don't meet my requirements. ...

Is it possible to hide a div using Media Queries until the screen size becomes small enough?

Whenever the screen size shrinks, my navbar sections start getting closer together until they eventually disappear. I've implemented a button for a dropdown menu to replace the navbar links, but I can't seem to make it visible only on screens wit ...

What is the best way to get process.argv to display only the arguments and exclude the node command and path?

As the title suggests, I am working on a substantial project that involves AppleScript and iMessage. After testing the script, it successfully opens Terminal and executes: node ~/Desktop/chatbot [argument]. However, at the moment, all it returns is: [ &apo ...

Troubleshooting Vue.js $emit Event Triggering without Any Result

I am facing an issue where I am attempting to trigger an $emit event from a child component in Vue to execute a function in App.vue. Despite the event being detected by both the Vue and DOM inspectors, nothing happens upon triggering it. I have gone throu ...

Error in strict mode: Undefined variable in Angular 2 inline template

My Angular 2 application has been throwing an error message stating "Error: Error in ./SampleComponent class SampleComponent - inline template caused by: Variable undefined in strict mode". Strangely, this error only occurs in IE 10. <input type="text" ...