Pattern to identify digits from 0 to 9 along with specific symbols

I am currently in the process of creating a regular expression that can match 0 to 9 (up to 10 digits) and /-. (0 to 2 max), without having to be all . or all //. It can be /.\- or .-. or any combination of the three, with a maximum of two of those characters.

My goal is to verify if a date is in specified formats. The user has the option to input 1 or 10.02, etc. I will separate that value and utilize it later on.

How can I construct the regex for this? The task is being carried out in Javascript.

I attempted var pattern =/^([0-9([\/.-]{0,2})]{1,10})$/g; before ultimately giving up.

Edit: 1 -> true 10.10 - true 10.12/2020 - true / -> false .10.20 - false

Answer №1

If you need to match a specific pattern in your text, you can use quantifiers like {0,2} to capture certain groups of characters.

To ensure that you only match up to 10 digits and not more, you can implement a negative lookahead in your regex expression.

For a maximum of 0 - 3 occurrences, you can adjust the quantifier accordingly to {0,3}

^(?!(?:[0-9\/.-]*[0-9]){11}$)[0-9]+(?:[\/.-][0-9]+){0,2}$

Check out this Regex demo for further clarification.

const samplePattern = /^(?!(?:[0-9\/.-]*[0-9]){11}$)[0-9]+(?:[\/.-][0-9]+){0,2}$/;

[
  "1",
  "10.02",
  "1111111111",
  "111.111.1111",
  "10.12/2020",
  "/",
  ".10.20",
  "11111111111",
  "111.111.11111"
].forEach(str => console.log(str + " --> " + samplePattern.test(str)));

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: Exceeded the maximum call stack size - What causes this and how can it be prevented?

I am currently utilizing standard JavaScript. As the title implies, I am encountering a RangeError and I'm unsure of how to prevent it. The issue at hand is that I shouldn't be receiving this error, and in reality, if I refresh the page there&ap ...

What is the best way to retrieve the initial cell from a row that the user is currently hovering over?

Is there a way to extract the first cell from a row when a user hovers over it and then clicks a specific key combination? (considering using jQuery) I have a table set up similarly where placing the mouse over a tr and pressing ctrl+c will copy the ID t ...

The scrolling function halts as soon as the element that was middle-clicked is deleted from the

I am knee-deep in developing my own React virtualization feature and encountering a minor annoyance. When I middle click on an item in the list and start scrolling, the scrolling stops once that item is removed from the DOM. My initial thought was that the ...

Inquiring about building a comprehensive AJAX website: SEO, Google index, design templates

I'm currently developing a fully AJAX-based website. My focus right now is on implementing page navigation and content display without the need for page reloading. Here's my approach: <html> <head> <!--CSS--> .hidde ...

Allow users to select options after they click a radio button

I have a pair of radio buttons and two sets of select options within different classes. Upon selecting the first radio button, the select options belonging to class1 should be enabled. On the other hand, upon selecting the second radio button, the select ...

What are some strategies for optimizing Next.js applications for mobile devices?

https://i.stack.imgur.com/mWmWG.png After realizing that the structure of my Next.js app doesn't align with Create React App's folder system, I'm stuck on how to effectively scale my website for mobile devices. In my confusion, I'm una ...

Triggering an automatic pop-up window using JavaScript based on a PHP condition

I have developed a hidden pop-up box that becomes visible when targeted, with its opacity increasing to one. I am aiming for the pop-up to appear if the user is identified as a "firstTimeUser," a status saved in a PHP $_SESSION variable. However, I am stru ...

The sequence of Angular directives being executed

When multiple directives are applied to an element in AngularJS, what determines the order in which they will be executed? For instance: <input ng-change='foo()' data-number-formatter></input> Which directive, the number formatter ...

What is the best way to perform a partial match query in Elasticsearch?

Can anyone provide me with a solution for extracting the word "google" from a link like http://drive.google.com? Currently, I am using the following search query: query: { bool : { must: { match: { text: 'google'} ...

Using a combination of functions to ensure synchronicity in JavaScript operations

Here is the function I have created: $scope.saveManualResendDraft = function(todo) { if ($scope.editMode) { updateStartJobManual(); byeSendManualInputDirectly(); } else { console.log('bye'); } }; I have defin ...

What is the most efficient method for storing text in my web application?

Hey there, I'm looking for a way to easily store and access the wording used in my application so that I can use them again whenever needed. For example, loading messages, validation messages, or informational messages are some of the wordings I want ...

How can I obtain hosting XULDocument for a ContentWindow or HTMLDocument within a Firefox extension?

I have a Firefox Extension with a XUL overlay that includes some Javascript with event handlers. One of these event handlers is an EventListener for DOMContentLoaded. I want to load a second overlay only when visiting a specific website. Here is a snippet ...

How can I show a tooltip in vuetify when a button is disabled?

I am currently using the button and tooltip components in my Vuetify project. I am trying to find a way to only display the tooltip when the button is disabled, but I'm having trouble figuring out how to do it correctly. Right now, the tooltip only a ...

The wrapAll() method can be used to wrap list items within two columns

I am looking to group multiple li elements within two div containers by using jQuery's wrapAll method. The challenge lies in the fact that these items are rendered within a single <ul> element via a CMS. Here is the current setup: <ul> ...

Verify the grid of buttons in my code for the background color

My goal is to make sure that when all the buttons are black, the h2 text in the 'lightsoff' div is displayed. If any buttons are not black, the text will remain hidden. I plan to achieve this by creating a new function that checks the background ...

What causes the variance in the node_modules directory between a project being installed locally versus globally?

Imagine I have a project called X, which depends on another package, Y. Y in turn has its own dependency on Z. When I run npm install Y in my project, the structure of my node_modules folder is as follows. Take note that Z is installed as a direct depend ...

JavaScript enables logging on Android Emulator

Currently, I am working with an Ionic app that is connected to SalesForce Mobile SDK. Due to the lack of support for the SDK and certain plugins in Ionic Serve, I have resorted to running the app in Android Studio using an Emulator - specifically, the Andr ...

Deactivating controls while displaying the loading bar in AngularJS

I'm currently working on a web application using AngularJS. I want to incorporate a loading bar to signify long data load times from the server. To replicate heavy data loads, I am utilizing $timeout to trigger the loadbar when the operation begins a ...

Using JavaScript to parse an XML document on a WAMP server

I am a beginner in the field of web development and currently struggling with parsing an XML document using JavaScript on WAMP server. I know that both the web page and XML file need to be on the same server for it to work. I tried placing both the PHP and ...

What seems to be the problem at hand, and where exactly is the issue located?

import React from 'react'; const card = ({name, email, id}) => { return( <div className='tc bg-light-green dib br3 ma2 grow bw2 shadow-5'> <img alt="robots" src = {'https://uniqueimage. ...