Guide on developing a regular expression to verify an identification number containing either three dots or dashes

users should only be allowed to enter IDs in the format shown below

1

1.1 or 1-1

1.1.1 or 1-1-1

1.1.1.1 or 1-1-1-1

I came up with this regex pattern:

/^(\d+(?:-\d+)*$)|(^\d+(?:\.\d+)*$)/

The current output allows an unlimited number of matches. However, I want to limit it to a maximum of 4 numbers.

1.23.4.55 or 1-23-4-55 are acceptable

but 1.1.1.1.1, 1-1-1-1-1, or 21.34.55.5.5.5.5 should be rejected

Despite my efforts, the regex still considers these inputs as valid

Answer №1

To easily implement this regex pattern, you can utilize the following:

^\d+([.-]\d+){0,3}$

In case you prefer not to combine separators, try the following alternative:

^\d+((-\d+){0,3}|(\.\d+){0,3})$

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

Most effective approach to setting up a timer using Javascript and PHP

I'm currently working on a quiz application using PHP and Javascript. The quiz begins once the user clicks a submit button, but I want it to end after a specific duration that I define. I'm hesitant to rely solely on the client-side clock as it c ...

Utilizing jQuery to implement a CSS style with a fading effect, such as FadeIn()

I have a jQuery script that applies a CSS style to an HTML table row when the user clicks on a row link: $(this).closest('tr').css("background-color", "silver"); Is there a way to soften this color change effect, such as by gradually fading in ...

Transform non-serializable JSON-like data into a structure that can be easily parsed for extracting values in Python

When working with Python scripts to handle JSON data, I encountered an error while trying to load the data. The initial simple script resulted in an error stating that the data was not JSON serializable. To address this issue, I modified the script to a m ...

Strategies for setting up the runtime dynamically for Nextjs API routes

After deploying my Next.js 13 app on Cloudflare Pages, I encountered an issue with the API routes. To address this, I had to export the runtime variable from each route in the following manner. export const runtime = "edge" During local developm ...

Issue with a stationary directional light tracking the movement of a rotating object and/or changes in the camera perspective

I've been facing a challenge in implementing a day-night cycle with a directional light in an Earth model using custom shaders. Everything seems to work fine with the night and day maps, as well as the light, as long as I don't manipulate the cam ...

Clicking on Google Code Prettify does not trigger syntax highlighting

I utilize Google Code Prettify for syntax highlighting my code. Below is the HTML snippet I use: <head> <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script> </head> <body> ...

Is there a way for me to generate a button that directs users to a variety of pages?

How can I create a button that takes me to a random page on my basic website when clicked? I'm having trouble changing the "href" attribute so that it links to a specific page. This is the JavaScript code I've come up with: document.addEventList ...

Ajax request returns a 500 error, yet the request successfully executes

I have set up an API controller to manage an ajax request. Each time the Ajax request is sent from the script below, I encounter a 500 error: POST http://localhost:58463/api/Reservations 500 (Internal Server Error) jquery-2.1.0.min.js:4 l.cors.a.crossDomai ...

How to link a CSS file from a JavaScript file

I have a form for users with fields for first name, last name, password, and confirm password. I've implemented validation to check if the password and confirm password fields match using JavaScript: $(document).ready(function() { $("#addUser").cl ...

Need to import Vue from a JavaScript file

I am relatively new to modern frontend development tools. After installing Nodejs and NPM, I successfully downloaded packages such as "jquery" and everything was working fine. However, when I installed Webpack (version 2) and created a demo configuration f ...

Leveraging AJAX for sending information to an API

A script I've written utilizes AJAX to communicate with a PHP-based API. The first part of the script is responsible for loading trade history: $(document).ready(function () { var orders = $('#History ul'); ...

Exploring the implementation of `setInterval` within the scope of a particular component in Vue

How can I restrict the scope of setInterval to a specific component only in Quasar framework when using SPA mode? Setting the function causes it to run everywhere, even when the page's URL is changed. I have already checked a similar question on Stac ...

Tips for resolving the trigger problem with onChange in the PinInput Component of chakra-ui

The PinInput Component's onChange event is not functioning properly with the value in Chakra-UI. This issue causes the focus to automatically shift to the next input even when the value hasn't changed. For instance, when attempting to set the st ...

Add Firebase Data to Dropdown

Utilizing Vuetify to build a dropdown has been an interesting challenge for me. While I can successfully select a value and store it in the firebase database, I'm facing difficulties when it comes to repopulating the dropdown after page refresh. Belo ...

Deciphering intricate JSON findings from Neo4j

First and foremost, I want to express my gratitude for taking the time to read this post. Secondly, I apologize in advance for its length, but as with a well-written function, the valuable information is right at the beginning - although I do hope you will ...

Having trouble connecting the HTML file with the JavaScript file

This is my unique HTML file <!DOCTYPE html> <html> <head> <script src="ll.js"></script> </head> <body> <link rel="stylesheet" href="home.css"> ...

Choose individual characters one by one

I am having issues with selecting the day, month, and year separately to calculate the days until due. It seems like my substr function may not be working correctly. Can you help troubleshoot why this is happening? http://jsfiddle.net/infatti/XeqPT/15/ f ...

I updated the script to include a feature that automatically adds a leading zero to hours, minutes, and seconds if they are less than 10. However, for some reason, the output still doesn't show the leading zero

I have successfully created a countdown timer that works effectively. One of the conditions I added is to display leading zeros for hours, minutes, and seconds if they are less than 10. The desired format is like this (06 : 08 : 09) instead of (6 : 8 : 9 ...

Enable button functionality when radio button is selected

Recently, I developed a view that showcases the following code: <ion-view view-title="Training Detail"> <ion-content class="padding"> <ul class="list"> <ion-item class="item item-checkbox item-button ...

HTML background image not displaying in JSPDF addHTML function

I am facing an issue with my HTML page where the background image in the header section is not displaying when converting the page to PDF using jspdf. Although all the other content appears correctly, the background image seems to be missing. You can vie ...