JavaScript regular expression for maximum decimal value of 7 digits with 2 decimal places

I am struggling to figure out how to validate the input in a textbox using a JS regular expression validator when working with a decimal set up as decimal(7,2) in the database.

function ValidateBoxes(input) {
    var valid = (input.match(/^\d+(?:\.\d+)?$/));

    if (valid = null) {
        alert("Decimals should be within 99999.99 range")
    } else {
        return true;
    }
}

Answer №1

match function is designed to extract grouped matches for further processing. However, if you simply want to check if the input matches the regex pattern, it's better to use regex.test instead of string.match. Here's an example:

/^\d{1,5}(?:\.\d{1,2})?$/.test(input); // returns true or false

Answer №2

One possible solution is to utilize:

input.match(/^\d{1,5}(?:\.\d{1,2})?$/)

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

Using PHP's preg_replace() function to handle multiple items

This is where I currently stand: <?php $text = preg_replace('/((\*) (.*?)\n)+/', 'awesome_code_goes_here', $text); ?> I have successfully identified plain-text lists in this format: * list item 1 * list item 2 My goa ...

How can we divide the nested JSON string in Node.js and append it to the existing JSON object?

Utilizing the node.js API, I have retrieved data from the database in a nested JSON string format. The "file_Name" field currently displays as "3.jpg, 2.jpg, 1.jpg", but my goal is to have it displayed as an array like this: "file_Name": ["3.jpg", "2.jpg", ...

CSS and Javascript functioning correctly within internal server, but encountering issues when accessed externally

I am in the process of creating a website for a friend. The goal is to have a flashy animated style website that functions well on IOS and allows him to make changes easily. To achieve this, I am utilizing JQuery, my own javascript, and multiple css files. ...

Eliminate apostrophes in a string by using regular expressions

Can someone help me with this word What is The Answer? I need to eliminate the space and apostrophe '. To remove spaces, should I use this method: data.text.replace(/ +/g, ""). But how can I remove the apostrophe? ...

Javascript: A Fun Game of Questions and Answers

When using JavaScript exclusively, I have an array consisting of four questions, four correct answers, and four incorrect answers. The use of arrays is essential to maintain order in the data. As each question is displayed, a random number is generated by ...

Randomly selecting JavaScript with different likelihoods or percentages

I am currently running a Node.js process with setInterval that occurs every 100ms. Within this process, I have specific actions that I want to execute at varying intervals such as 2% of the time for action X and 10% of the time for action Y. Currently, my ...

The error message "express-validator - req.checkBody does not exist" indicates that the

After spending the last 2 hours trying to resolve this issue, I'm starting to feel frustrated. Essentially, I am attempting to validate a form input, but I keep encountering the following error message. TypeError: req.checkBody is not a function at ...

Prevent form submission when email is missing

function validateEmail(){ var TCode = document.getElementById('email').value; if(TCode.length==0) { email_info.innerHTML="This field is required"; return false; } email_info.innerHTML=" "; ...

Ways to retrieve the date of the chosen <td> cell in a calendar

Looking for a way to extract dates from the <td> elements selected by mouse? Here is my code snippet that highlights the TD upon selection: $(function () { var isMouseDown = false, isHighlighted; $("#schichtplan td") .moused ...

`Active the button once user checks one or more checkboxes using AngularJS`

I'm working on a project where I have checkboxes and a button. I need to figure out how to activate the button when one or more checkboxes are checked using AngularJS. Can anyone provide some guidance? <script src="https://ajax.googleapis.com/aj ...

Initiate function directly through computed property (Vue.js)

I'm currently working on a project using Vue 3 along with VueX. I am wondering if there is a way to check within the computed property whether the value returned is true, and then directly trigger my method without needing a watcher. A bit of backgr ...

Compare the array against the keys to filter the object

Currently, I have an object with keys that have multiple values. Additionally, there is an array with a simple key check as follows: ["random", "three"]. My goal is to retrieve the mainData, but only with the object and the data that match the keys in th ...

Why is the Angular directive '=&' value binding to the scope showing as undefined?

An Angular directive has been defined within my application: (function() { 'use strict'; angular .module('almonds') .directive('security', ['$animate', 'AuthFactory', directive]); fun ...

Identify strings that contain and do not contain a specific word using the stringr package

Hi there, I'm new to this and having trouble finding a solution to my question. I have a string variable with various observations and I want to detect MS, MA, or Master while excluding MBA: data <- c("Master of Business Administration (MBA) progr ...

Using Node.js to pass the response object to a Bull queue for server-side events

I'm facing a dilemma with an architectural decision. Currently, I have a Node + Express app with an API for file uploads. Once a file is uploaded, it is processed in batches by FFMPEG using Bull Queue + Redis. This setup functions well, but I've ...

Creating an animated sidebar that moves at a different speed than the rest of the

I have a layout with a large sidebar and small content, where the "Fixed part" refers to sticky positioning. I'm attempting to achieve this using scrollTop, but the sidebar is causing some issues like this: The code should only be executed when the ...

How to organize and reuse code efficiently with Node.js, EJS, and front-end JavaScript?

It seems that I may have chosen the wrong platform by posting this question on the 'Software Engineering' exchange: Currently, my focus is on learning the MEAN stack (I have yet to dive into Angular, so for now I am using pure vanilla JS for the ...

Program for sending and receiving messages

The concept behind this program is to enable users to send messages. The user navigates to the web interface, selects their ID as the sender and chooses a receiver ID before pressing submit. Upon submission, they are directed to a page displaying all the m ...

Issue with D3.js call() Method

I am in the process of constructing a D3JS chain that concludes with a .call() function. Below is my current chain: foo.selectAll('svg') .data(data) .enter() .append('svg') .classed('someclass', true) .cal ...

Is there a way to modify the maximum size limit for a POST request package?

I am encountering an issue while attempting to send an array of bytes using a POST request. In my server-side implementation, I am utilizing Node.js and Express.js. Unfortunately, I am receiving error code 413 or the page becomes unresponsive ('Payloa ...