The validation for decimal numbers fails to function when considering the length

I've been struggling to come up with a regular expression for validating decimal numbers of a specific length.

So far, I've tried using

pattern="[0-9]){1,2}(\.){1}([0-9]){2}"
, but this only works for numbers like 12.12.

What I'm aiming for is a pattern validation for (13 digits).(6 digits) and also considering the total length.

Here's what I expect:

      `1234567891123.123456` //true
      `1234567891123123456`  //false since it contains only numbers
      `12345678911234.123456` //false because it has 14 digits before the decimal point
      `1234567891123.1234567` //false due to having 13 digits before the decimal point

Can anyone suggest a more suitable regex that meets the criteria mentioned above?

Answer №1

If you need to test regular expressions, a great resource is

\b\d{13}\.\d{6}\b

Answer №2

To match the desired criteria, the regex pattern should be structured as follows:

([0-9]){13}(\.){1}([0-9]){6}

For practical application in an HTML form input field, it would appear like this:

<form action="">

  <input type="text" required pattern="([0-9]){13}(\.){1}([0-9]){6}" />

  <button type='submit'>Submit</button>
</form>

Answer №3

Here is a suggestion you could consider:

\d{13}[.]\d{6}

Answer №4

Live Example :

const pattern = /^[0-9]{13}\.[0-9]{6}$/i;

function getInputValue()  {
    return document.getElementById("myinput").value;
}

function runTest() {
    alert(pattern.test(getInputValue()));
}
<input type="text" id="myinput"/>
<button id="testBtn" onclick=runTest()>Run Test</button>

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 encountered with Typescript and Request-Promise: Attempting to call a type that does not have a call signature available

I have a server endpoint where I want to handle the result of an asynchronous request or a promise rejection by using Promise.reject('error message'). However, when I include Promise.reject in the function instead of just returning the async requ ...

methods for transforming JSON array output objects into individual non-array values

I'm facing an issue with a JSON result that contains latitude and longitude as an array like [13.0801721, 80.2838331]. I need help in converting this to comma-separated values without the array brackets, similar to 13.0801721, 80.2838331. This is my ...

Tips for troubleshooting TypeScript integration tests within Pycharm

Currently, I have been utilizing PyCharm to code in typescript. To run tests in the integration directory, I execute npm run test:integration. However, I am now looking to debug the tests. The directory structure looks like this: my_project /src /tests ...

Leveraging React's useEffect hook to asynchronously fetch and load data

In my coding scenario, there is a parent component containing a child component which loads data asynchronously. This is what I currently have: <Parent> <AsyncChild data={props.data} /> <Child /> </Parent> Within the AsyncChil ...

The ball refuses to fall into the designated boxes

I designed a basic webpage featuring 3 boxes that, when clicked on, trigger the dropping of a ball into them. Below is the code I used: <!DOCTYPE html> <html> <head> <script type="text/javascript"> function popup (n) { ...

Post-Angular migration (going from version 12 to 14), there are still lingering security risks associated with the "d3-color vulnerable to ReDoS" issue

Following the migration to Angular 14, I made sure to update my "@swimlane/ngx-charts" version to "20.1.2" and the "d3" version to "7.8.4". However, even after running the npm install command, I am still encountering 5 high severity vulnerabilities in my p ...

Basic asynchronous JavaScript and XML (AJAX) call

I am currently learning about ajax and experimenting with a script that involves extracting information from a JSON object and displaying it on the document. Below is an example of the JSON file named companyinfo.json: { 'id':1, 'name&apos ...

What is the method for retrieving a specific value following the submission of an AngularJS form?

Here is how my form begins: <!-- form --> <form ng-submit="form.submit()" class="form-horizontal" role="form" style="margin-top: 15px;"> <div class="form-group"> <label for="inputUrl" class="col-sm- ...

Trouble locating Angular module while referencing script as an ES6 module

Check out this simple plunk here. My goal is to reference the app.js file as an ES6 module by setting the type attribute of the script tag to module. However, when I add the type attribute, Angular is unable to find the module. If I remove it, then it work ...

What could be causing the responsive line chart from nivo to not appear in jsdom while using Jest?

I am currently working on writing UI tests for my application using Jest/Testing Library. In the testing process, I have integrated the ResponsiveLine component from the @nivo/line library. However, I am facing an issue where the Responsive Line componen ...

Javascript recursive method for fetching data entries

Seeking a solution to retrieve interconnected records based on a parent column, where the relation can be one or many on both ends. After attempting a recursive function without success, I found my code became overly complex and ineffective. Is there a st ...

Angular: controller's property has not been initialized

My small controller is responsible for binding a model to a UI and managing the UI popup using semantic principles (instructs semantic on when to display/hide the popup). export class MyController implements IController { popup: any | undefined onShow(con ...

jQuery can be utilized to generate repeated fields

$(document).ready(function(){ $('#more_finance').click(function(){ var add_new ='<div class="form-group finance-contact" id="finance_3"><div class="col-sm-9"><label for="firstName" class="contro ...

The specified 'Object' type does not match the required 'Document' constraint

I need assistance with running a MERN application to check for any issues, but I keep encountering this error across multiple files. Error: The 'CatalogType' type does not meet the requirements of 'Document'. The 'CatalogType&apo ...

Interactive Vue tree view featuring draggable nodes

I am currently working on creating a tree component in Vue.js, and I am facing challenges with implementing drag and drop functionality in the Tree component. I am unsure where to begin, as I have looked through similar code on GitHub but I am struggling t ...

Angular: merging multiple Subscriptions into one

My goal is to fulfill multiple requests and consolidate the outcomes. I maintain a list of outfits which may include IDs of clothing items. Upon loading the page, I aim to retrieve the clothes from a server using these IDs, resulting in an observable for e ...

jQuery: add to either element

What is the most efficient way to use .appendTo for either one element or another based on their existence in the DOM? For instance, I would like to achieve the following: Preferred Method: .appendTo($('#div1') || $('#div2')); Less ...

The combination of sass-loader and Webpack fails to produce CSS output

Need help with setting up sass-loader to compile SCSS into CSS and include it in an HTML file using express.js, alongside react-hot-loader. Check out my configuration file below: var webpack = require('webpack'); var ExtractTextPlugin = require ...

Tips for adding an svg element to an existing svg using d3.js

Can another SVG be appended to an existing SVG parent using d3.js? I have tried using the 'svg:image' attribute, but unfortunately, I lose full control over the inner SVG child. The DOM node is created by d3, but it is not rendered, resulting i ...

Using the parameter value as a property name in the return type of a function in TypeScript: a guide

After creating a function that converts an object to an array where each element contains the ID of the object, I encountered a new requirement. The current function works great with the following code: const objectToArray = <T>(object: { [id: string ...