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
Related
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.
I want to make a regex pattern for number input. When user enter numbers the comma is automatically separate entered number like 1,424 or 23,232. This is working fine with regex pattern
/^[0-9.,]*$/
But the problem is that this pattern allowed dot(.) between numbers. I want to make regex expression like the input can allowed .00 ate the end of numbers not between numbers. But the .00 is also optional.
Allowed number formats are below:
123312131256457.00
1233121312564
9,223,372
Not allowed number formats are below:
34.343455.3434
34353...
I spend lost of time on same but does't get any solution. Please share yours ideas. Thanks in advance.
Try this regex:
/^[0-9]{1,3}((,[0-9]{3})*|([0-9])*)(\.[0]{2})?$/
Here is a brief explanation:
^ from the start
[0-9]{1,3} match 1 to 3 numbers
(,[0-9]{3})* followed by a comma and three numbers, any number of times
([0-9])* OR just followed any amount of numbers, with NO commas
(\.[0-9]{2})? followed by an optional decimal point and two zeroes
$ end
Demo here:
Regex101
The very, very simple answer would be /^[0-9,]*(\.00)?$/. I.e., add an optional .00 suffix, remove support for . literal before the optional part.
As commented above you could go for something more fancy: ^(0|[1-9]\d{0,2}(,?\d{3})*)(\.00)?$
This will behave as commented:
0 // OK
01 // Not OK, must start with 1-9 if not 0 or 0.00
1,1,1.00 // Not OK, groups must be 3 digits, if used
0,000.00 // Not OK, should be 0.00
1,000 // OK
123312131256457.00 // OK, groups are optional
1233121312564 // OK, decimals (.00) are optional
9,223,372 // OK
just something like
/^[0-9,]*(?:\.0+)?$/
but [0-9,]* also allows "3,,,"
I am trying to write a regular expression for a bit of javascript code that takes a user's input of a mobile number and in one regular expression, performs the following checks:
Starts with 07
Contains only numbers, whitespace or dashes
Contains exactly 11 numbers
Is this possible to do in just one regular expression and if so, how please?
I don't think it is possible with one regex, but it is possible by testing for two conditions:
if(/^07[\d\- ]+$/.test(str) && str.replace(/[^\d]/g, "").length === 11) {
//string matches conditions
}
Explanation of the regex:
^: Anchor that means "match start of string".
07: Match the string 07. Together with the above, it means that the string must start with 07.
[: Beginning of a character class i.e., a set of characters that we want to allow
\d: Match a digit (equivalent to 0-9).
\-:
" ": Match whitespace (markdown doesn't let me show a single space as code)
]: End of character class.
+: One or more of the previous.
$: Anchor that means "match end of string". Together with the ^, this basically means that this regex must apply to the entire string.
So here we check to see that the string matches the general format (starts with 07 and contains only digits, dashes or spaces) and we also make sure that we have 11 numbers in total inside the string. We do this by getting of anything that is not a digit and then checking to see that the length of the string is equal to 11.
Since #Vivin throws out the challenge :
/^07([-\s]*\d){9}[-\s]*$/
^07 : begin with digits 07
( : start group
[-\s]* : any number of - or whitespace
\d : exactly one digit
){9} : exactly 9 copies of this group (11 digits including 07)
[-\s]* : optional trailing spaces or -
$ : end of string
Of course a more useful way might be as follows
if ((telNo = telNo.replace (/[-\s]+/g, '')).match (/^07\d{9}$/)) {
....
}
which has the advantage (?) of leaving just the digits in telNo
Thank you all for trying, but after a good while trying different ideas, I finally found a working "single" regular expression:
07((?:\s|-)*\d(?:\s|-)*){9}
This make sure that it starts with 07, only contains digits, whitespace or dashes, and only 11 of them (9 plus the first 2) are numbers.
Sorry to have wasted your time.
Explanation:
() - include in capture
(?:) - do not include in capture
\s - whitespace
| - or
- - dash
* - zero or more
\d - digits only
{9} - exactly nine of what is captured
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.
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})*