Validate time and allow negative values [duplicate] - regex

This question already has answers here:
How do I include negative decimal numbers in this regular expression?
(17 answers)
Closed 6 years ago.
I use this regex to validate time entries, e.g. 08:34 - this is a valid time,
negative time e.g. -08:54 is not valid with this regEx.
Actually I don't know how to extend this regex in order to make also negative times valid:
var regex_time =/^([0-9]{2})\:([0-9]{2})$/;

You can use the regex:
^-?([01]\d|2[0-3]):([0-5]\d)$
To get a positive/negative valid 24h format. If you don't need to limit the user to 24h format, use:
^-?(\d+):([0-5]\d)$
The question mark is a quantifier that means 0 to 1 of previous item.

Related

What's the regular expression for pipe-delimited number? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to check that a String is pipe-generated numbers. There should be numbers between pipes.
Valid Strings examples: 300, 300|600.
Invalid Strings examples: 300||||600
I tried ^([\d|\d])*$. However, this still said that 300||||600 is a valid String.
Here, what you want is one number, ie \d+, followed by an undetermined number of occurrences of a pipe then a number, which would be (\|\d+)* (the pipe is escaped).
As you want it to cover the whole input this would be
^\d+(\|\d+)*$

Is there a standard way to determine only maximum length? [duplicate]

This question already has answers here:
Regular expression to limit number of characters to 10
(6 answers)
Closed 3 years ago.
I have a case that I only care about the maximum number.
For instance, consider that I have to simply check that the string is: "max of 10 numeric digits", which means that it should meet the following:
It contains only numbers (resolved).
It has to be 10 digits at maximum.
I read about limiting the length, I came up with the following result:
^\d{10}$: all numerics, 10 numbers specifically.
^\d{10,20}$: all numerics, 10 - 20 length.
^\d{10,}$: all numerics, 10 at minimum.
However, ^\d{,10}$ is invalid! Is there a specific way to do it, or should I do it as ^\d{1,10}$ ?
^\d{1,10}$ or anubhava's advice in the comment might simply suffice, yet there might be other excessive expressions such as:
^(?=[0-9]).{1,10}$
^(?=[0-9])\d{1,10}$
that might work.
Demo

Regex value of 0.0-5.0 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 5 years ago.
I'm trying to create a regex string that allows values 0.0 - 5.0. I need the one decimal point to be required. The string below gets me there, but it also allows 5.1-5.9. How do I prevent 5.1-5.9 from being entered, and allow 5.0?
^[0-5]+(\.[0-9]{1})$
Try this regex:
^([0-4]\.[0-9]|5\.0)$
It matches any number from 0 to 4 then dot then any number.
it also matches 5.0
Note: Your regex has another problem that you used + after [0-5] which also matches 55 for example, so you need to remove the +. You also need to remove {1}, It won't make any change but it's useless.

RegEx to get numeric value and values with range [duplicate]

This question already has answers here:
How to write regex to match number ranges?
(7 answers)
Closed 8 years ago.
I need a RegEx to match the following format:
3
7.3
5.7 (2.2-10.4)
I want to check in JS if the given value i.e. 5.7 (2.2-10.4) is valid. And in PHP I want to split the string to get all three values (if there is a given range).
That means I have to check for a simple numeric value (int and float) or a numeric value followed by a range like (d-d)
My attempt: /^(\d+)\s\((\d+)\-(\d+)\)$/
based on your attempt, does not count for negative values
^(\d+(?:\.\d+)?)(?:\s*\((\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)\))?$
Demo

Regex for range 1-1000 [duplicate]

This question already has answers here:
Regular expression where part of string must be number between 0-100
(7 answers)
Closed 1 year ago.
I need help creating a simple regex for a whole number range of 1-1000, with no special characters.
The two I have both seem to break or allow characters or not the full range:
^\d(\d)?(\d)?$
^[0-9]{1,3}$
Try this:
^([1-9][0-9]{0,2}|1000)$
[1-9][0-9]{0,2} matches any number between 1–999
1000 matches 1000
Use ^(.*[^0-9]|)(1000|[1-9]\d{0,2})([^0-9].*|)$ which will match 1000 or a non-zero digit followed by up to two further digits. It will also allow other characters on either end of the number.