JS Regex to allow only numbers, semicolon and hyphen - javascript

I am building an application which should accept strings only with the following formats:
12345 (only a number)
12345;23456 (two or more numbers separated by ;)
12345-12367 (a range of numbers separated by a -)
The java script regex should allow only the above formats & shouldn't accept any other formats or symbols . Can anyone come up with a regex for this?

This is the RegExp that you need: /^\d+((;\d+)*|-\d+)?$/
(;\d+)* will check for multiple numbers separated by ";"
-\d+ will check for a range

Try
^[0-9]+([;-][0-9]+)?$
That should work
[0-9]+ matches 1 or more digits
[;-] matches a ; or a -
(...)? is an optional match
^ anchors the start and $ anchors the end of the string

^[0-9-;]{0,50}$
0-9 only accept numbers
-; allow only - and ;
{0,50} allow only 50 chars

Assuming that the number portions you are looking for are 5 digits each time, the following should match what you want.
[0-9]{5}((;|-)[0-9]{5}){0,1}
If you need different lengths, you can update the {5} with either another fixed length or a range such as {3,5} for a string of 3 to 5 digits. If you want to be able to capture more than two numbers with the speperators listed, you can use
[0-9]{5}((;|-)[0-9]{5})*

Related

Regex for a combination of alphanumeric characters and range of numbers

I need a regex for PORT input, it must allow only inputs like this:
gei_1/8 or xgei-0/7/0/5
-It must allow underscore and hyphen only at the start: gei_1 or xgei-0
-Then it must allow two digit numbers within a range of 0-48, separated by forward slash with no spaces between them. No more than three numbers: /7/0/48
-It cannot allow forward slash or anything at the end
Right now, I have the following Regex for the alphanumeric part: /^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/
For the number part I have: ^(\d|1\d|2\d|3\d|4[0-8])\/(\d|1\d|2\d|3\d|4[0-8])\/(\d|1\d|2\d|3\d|4[0-8])$
Thanks for the help
To repeat the forward slash 1 - 3 times you could use a range to match 0-48 and repeat that 1-3 times using a quantifier {1,3}
(?:\/(?:[0-9]|[1-3][0-9]|4[0-8])){1,3}
The full pattern could look like
^[A-Za-z][A-Za-z0-9]*[_-][A-Za-z0-9]+(?:\/(?:[0-9]|[1-3][0-9]|4[0-8])){1,3}$
Regex demo

Javascript regex to make sure that string matches format x:y

I am trying to parse a string which has two numbers, both can be between 1 and 3 digits, and will have a colon in between. Here are some examples:
"1:1"
"1:12"
"12:1"
"123:12"
Also, the given string may also be invalid, and I need to detect if it is. My attempts so far to make sure the string is valid have looked like this: .match(/[1-9]\:[1-9]/);. But then I noticed that this wont work if a string such as this is inputted: "characters12:4characters". How would I go about validating the string to make sure it is in the format x:y?
Any help would be deeply appreciated.
Edit: numbers which contain 0 at the beginning is valid, but may not be given.
You may use
/^\d{1,3}:\d{1,3}$/
See the regex demo
Details
^ - start of a string
\d{1,3} - one, two or three digits (\d is a shorthand character class that matches any digit (it can also be written as a [0-9] character class) and {1,3} is a limited quantifier that matches1 to 3 consecutive occurrences of the quantified subpattern)
: - a colon
\d{1,3} - one, two or three digits
$ - end of the string.

regex to not allow two zeros in the end of a number

I am trying to implement input number field which will not allow if number end with two 0's i.e. if I enter 23100 then it should not allow it.
I'm using the regex /^[0-9]*00$/ but it allowing 123100.
I have pasted my code
enter code here
You may use
/^(?!\d*00$)\d+$/
It will match
^ - start of string
(?!\d*00$) - a negative lookahead that makes sure no 0+ digits followed with 00 at the end are allowed
\d+ - one or more digits
$ - end of string
Updated now to now use look-behinds or look-aheads.
^\d*(?:\d?[1-9]|[1-9]\d)$
Demo
try this.
(^\d+[1-9]+0{0,1}$)
This will work for numbers like
12
123
12310
etc
any number with 2 or more 0's at the end doesn't match in this case

Validating phone number using regex with support for multuple numbers

I am not very experienced with regex and I need to validate phone numbers using javascript.
I have a textbox which need to be allowed to accept multiple phone numbers with a delimiter of ';' and the characters that can be allowed for the phone numbers are
Numbers
'+'
'-'
Could someone help me on how I can acheive this using javascript and regex/ regular expressions?
Example:
+91-9743574891;+1-570-456-2233;+66-12324576
I tried the following:
^[0-9-+;]+$
Am not sure if this is correct.
You have placed - in wrong place so, your regex is not working.
Try this(your RegEx, but slightly modified):
^[0-9+;-]+$
or
^[-0-9+;]+$
To include a hyphen within a character class then you must do one of the following:
escape the hyphen and use \-,
place hyphen either at the beginning or at the end of the character class.
As the hyphen is used for specifying a range of characters. So, regex engine understands [0-9-+;]+ match any of the characters between 0 to 9, 9 to +(all characters having decimal code-point 57[char 9] to 43[char +] and it fails) and ;.
To be a bit more restrictive, you could use the following regexp:
/^\+[0-9]+(-[0-9]+)+(;\+[0-9]+(-[0-9]+)+)*$/
What it will match:
+91-9743574891
+1-570-456-2233;+66-12324576
What it won't match:
91-9743574891
+15704562233
6612324576
How about this ^([0-9\-\+]{5,15};?)+$
Explanation:
^ #Match the start of the line
[0-9\-\+] #Allow any digit or a +/- (escaped)
{5,15} #Length restriction of between 5 and 15 (change as needed)
;? #An optional semicolon
+ #Pattern can be repeat once or more
$ #Until the end of the line
Only as restrictive as specified could be tighter, See it working here.
Your regex will match what you allow, but I would be a bit more restrictive:
^\+?[0-9-]+(?:;\+?[0-9-]+)*$
See it here on Regexr
That means match an optional "+" followed by a series of digits and dashes. Then there can be any amount of additional numbers starting with a semicolon, then the same pattern than for the first number.

Regular expression to match numbers, finite ranges, and infinite ranges (such as >=9)

What can be a regular expression for following type of string
E.g. 1, 2-3, 4..5, <6, <=7, >8, >=9
Here I am using equals, range (-), sequence (..) & greater than/equal to operators for numbers less than 100. These numbers are separated by a comma.
Pls help me in writing a regular expression for this. Thanks in advance.
Atul
How about something like this:
^(\d+(-|\.\.)|[<>]=?)?\d+$
Example using Python:
>>> import re
>>> pattern = '^(\d+(-|\.\.)|[<>]=?)?\d+$'
>>> for s in '1, 2-3, 4..5, <6, <=7, >8, >=9'.split(','):
... print(re.match(pattern, s.strip()).group(0))
...
1
2-3
4..5
<6
<=7
>8
>=9
To be clear, this regex matches only one element in the list. I highly recommend that you preprocess your input by splitting it on commas and trimming the individual elements, like I did in the example above. Even though that's not strictly necessary (you can add this logic to the regex I gave here), it will but quite a bit more efficient and readable.
How the regex works:
Observe that every valid string ends with one or more digits, thus \d+$.
There may or may not be something before that, thus, ^(...)?\d+$.
Those prefixes are either the start of a range, or a comparison:
\d+(-|\.\.) matches a number followed by a dash or two periods.
<=? matches "<" as well as "<=". Likewise for >=?. We can abbreviate this to [<>]=?.
Combining these two options using a pipe (|), which signifies choice, we get
^(\d+(-|\.\.)|[<>]=?)?\d+$
Try this expression:
^(?:\d+(?:(?:\.\.|-)\d+)?|[<>]=?\d+)(?:,\s*\d+(?:(?:\.\.|-)\d+)?|[<>]=?\d+)*$
It consists of the alternation of
\d+(?:(?:\.\.|-)\d+)? for a number followed by an optional expression for a range or sequence, and
[<>]=?\d+ for the inequalities.
That’s repeated in the second parts with a comma and optional whitespace for the list.
And for the condition of only allowing numbers less than 100, you can replace \d+ with [1-9]\d for 1..99 or (?:0|[1-9]\d) for 0..99.
You should totally use a regular expression tool like regex buddy.
You're trying to verify that your string generally looks like the sample?
1, 2-3, 4..5, <6, <=7, >8, >=9
matches
\s*(\d+|\d+-\d+|\d+\.\.\d+|[<>]=?\d+)\s*(,\s*(\d+|\d+-\d+|\d+\.\.\d+|[<>]=?\d+)\s*)*
It's easier to split on , and then match each part with
\s*(\d+|\d+-\d+|\d+\.\.\d+|[<>]=?\d+)\s*
That reads:
white space trimmed, match digits or digits dash digits, or digits dot dot digits, or one of less-than or greater-than with optional equal to digits.
You can compress that down to the harder to read:
\s*((\d+(-|\.\.)|[<>]=?)?\d+)\s*
If you want all your digits to be 1-2 digits only, then change all the \d+ to \d{1,2} or \d\d?

Categories

Resources