RegEx/Javascript validation: Don't allow comma as a valid character - javascript

I'm doing Javascript validation on my number fields. I'm using RegEx to do this - first time RegEx user. I need the user to only enter numbers and decimals, but not commas. (i.e. 3600.00 is okay, but 3,600.00 is not). I can't figure out how to eliminate the commas as an accepted character. Any help is appreciated. Thanks!
var filter = /^([0-9])/;
if (!filter.test(txtItemAmount.value))
{
msg += "Item amount must be a number.\n";
txtItemAmount.focus
}

If you want to allow decimals less than 1, integers or integers with a decimal part, you can write a reg exp for that-
/^(\.\d+)|(\d+(\.\d+)?)$/.test(value)
or you can use parseFloat-
if(parseFloat(value)+''===value)

Your filter should be something like [0-9. ]+ (here you allow numbers, . and space
A better filter would be [0-9 ]*[ .][0-9 ]* where you allow . only once.
I don't know about regex in javascript, so you may need to protect some characters with \.

try this:
^(\d+(\.\d*)?)$
It looks for one or more digits (\d+) and then, for a period followed by 0 or more digits ((\.\d*)?) . The question mark means that there has to be either 1 or 0 repetitions of the period and more digits part. THe period (.) is a special character in regex, so it has to be escaped, hence the \ before hand.
For more information, you might want to take a look here

^(\d+\.?|\d*\.\d+)$
will allow 1, .2, 3., 4.5, 12345.67890 and so on; it will disallow 1,000, 0.123,456, . or 1.2.3.

Using "lookaround," a combo of negative lookbehind and look ahead, you should be able to fail the match if a comma is present:
(?<!,)[0-9\.]*(?!,)

Related

Regex expression for numbers with commas and optional .00

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,,,"

RegEx a name with special characters in javascript

I'm relative new to RegEx and I've encountered a problem. I want to regex a name. I want it to be max 100 characters, contain at least 2 alphabetic characters and it will allow the character '-'.
I have no problem to only check for alphabetic characters or both alphabetic characters and hyphen but I dont't want a name that potantially can be '---------'.
My code without check for hyphens is
var nameRegExp = /^([a-z]){2,100}$/;
An explanation for the code is appreciated as well.
Thanks!
I guess
/^(?=.*[a-z].*[a-z])[a-z-]{1,100}$/
the lookahead part (^(?=.*[a-z].*[a-z])) checks if there are at least two letters. This pattern ("start of string, followed by...") is a common way to express additional conditions in regexes.
You can limit the number of - by adding a negative assertion, as #Mike pointed out:
/^(?=.*[a-z].*[a-z])(?!(?:.*-){11,})[a-z-]{1,100}$/ // max 10 dashes
however it might be easier to write an expression that would match "good" strings instead of trying to forbid "bad" ones. For example, this
/^[a-z]+(-[a-z]+)*$/
looks like a good approximation for a "name". It allows foo and foo-bar-baz, but not the stuff like ---- or foo----bar----.
To limit the number of - you could add a negative look-ahead, where the number 3 is one more than the maximum number you want to allow
/^(?!(?:[a-z]*-){3,})(?=-*[a-z]-*[a-z])[a-z-]{2,100}$/

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.

Regex to validate brazilian money using Javascript

I need to validate some form fileds that contain brazilian money (its name is "Real") using Javascript. It has the following format:
0,01
0,12
1,23
12,34
123,45
1.234,56
12.235,67
123.456,78
1.234.567,89
12.345.678,90
123.456.789,01
1.234.567.890,12
My regex knowledge is weak, can somebody help me please?
Does this do what you want?
^\d{1,3}(?:\.\d{3})*,\d{2}$
That says "1 to 3 digits, optionally followed by any number of groups of three digits preceded by a period, followed by a comma and two more digits." If you want to allow the leading whitespace present in your example, add \s* to the front:
^\s*\d{1,3}(?:\.\d{3})*,\d{2}$
EDIT: As #ElRonnoco pointed out, the above regular expression accepts leading zeroes (e.g. 010.100,00). To disallow those, you may use this longer version:
^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0),\d{2}$
EDIT 2 The above regular expressions all match a string containing a single monetary amount and nothing else. It's not clear from the question if that's the intent.
EDIT 3 To allow numbers that have no decimal part, or only one decimal digit, change it like this:
^\s*(?:[1-9]\d{0,2}(?:\.\d{3})*|0)(?:,\d{1,2})?$
I would give this regex a try:
\d+(?:\.\d{3})*?,\d{2}
What it says is:
- match digits until
a. a dot followed by 3 digits is found (and this step can be repeated several times)
b. or a comma followed by 2 digits is found
EDIT:
- thanks for the comments, I forgot about the constraint for the first value
updated regex
\d{1,3}(?:\.\d{3})*?,\d{2}
Complementing Mark's reply:
Who needs "." in the string and not "," to count cents. And need find the values in middle a text :
(?:[1-9]\d{0,2}(?:\,\d{3})*|0)(?:.\d{1,2})?
https://regexr.com/61166

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