Regex to validate brazilian money using Javascript - 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

Related

Regular expressions: prohibit the use of characters [duplicate]

I have a regex
/^([a-zA-Z0-9]+)$/
this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.
Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:
/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
This RE will do:
/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i
Explanation of RE:
Match either of the following:
At least one number, then one letter or
At least one letter, then one number plus
Any remaining numbers and letters
(?:...) creates an unreferenced group
/i is the ignore-case flag, so that a-z == a-zA-Z.
I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.
An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".
So, test for this :-
/^([a-zA-Z0-9]+)$/
Then this :-
/\d/
Then this :-
/[A-Z]/i
If your string passes all three regexes, you have the answer you need.
The accepted answers is not worked as it is not allow to enter special characters.
Its worked perfect for me.
^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$
one digit must
one character must (lower or upper)
every other things optional
Thank you.
While the accepted answer is correct, I find this regex a lot easier to read:
REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"
This solution accepts at least 1 number and at least 1 character:
[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
And an idea with a negative check.
/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
^(?! at start look ahead if string does not
\d*$ contain only digits | or
[a-z]*$ contain only letters
[a-z\d]+$ matches one or more letters or digits until $ end.
Have a look at this regex101 demo
(the i flag turns on caseless matching: a-z matches a-zA-Z)
Maybe a bit late, but this is my RE:
/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/
Explanation:
\w* -> 0 or more alphanumeric digits, at the beginning
\d+[a-zA-Z]|[a-zA-Z]+\d -> a digit + a letter OR a letter + a digit
\w* -> 0 or more alphanumeric digits, again
I hope it was understandable
What about simply:
/[0-9][a-zA-Z]|[a-zA-Z][0-9]/
Worked like a charm for me...
Edit following comments:
Well, some shortsighting of my own late at night: apologies for the inconvenience...
The - incomplete - underlying idea was that only one "transition" from a digit to an alpha or from an alpha to a digit was needed somewhere to answer the question.
But next regex should do the job for a string only comprised of alphanumeric characters:
/^[0-9a-zA-Z]*([0-9][a-zA-Z]|[a-zA-Z][0-9])[0-9a-zA-Z]*$/
which in Javascript can be furthermore simplified as:
/^[0-9a-z]*([0-9][a-z]|[a-z][0-9])[0-9a-z]*$/i
In IMHO it's more straigthforward to read and understand than some other answers (no backtraking and the like).
Hope this helps.
If you need the digit to be at the end of any word, this worked for me:
/\b([a-zA-Z]+[0-9]+)\b/g
\b word boundary
[a-zA-Z] any letter
[0-9] any number
"+" unlimited search (show all results)

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 - match range of characters with a quantifier that only matches numbers

I've found a similar question on SO, but nothing I can get my head around. Here's what I need;
6 or more digits, with these characters allowed \s\-\(\)\+
So here's what I have /^[0-9\s\-\(\)\+]{6,}$/
The problem is, I don't want anything other than the number to count towards the 6 or more quantifier. How can I only "count" the digits? It would also been good if I could stop those other allowed characters from being entered adjacently e.g:
0898--234
+43 34 434
After an hour of reading up and looking at a regex cheat sheet, I'm hoping some kind person can point me in the right direction!
You could do something like this:
/^([\s()+-]*[0-9]){6,}[\s()+-]*$/
This will match any number of special characters (whitespace, parentheses, pluses or hyphens) followed by a single decimal digit, repeated 6 or more times, followed by any number of special characters.
Or this if you don't want to match two or more adjacent special characters:
/^([\s()+-]?[0-9]){6,}[\s()+-]?$/
You can use lookahead:
/^(?=(\D*\d){6,})[0-9\s()+-]{6,}$/
/^[\s()+-]*(\d[\s()+-]*){6,}$/
This doesn't count the 'cruft'. It allows any number of special characters, followed by six times [a digit followed by any number of special characters].
If you want max. one special character in between digits, use ? instead of *, but I assume you don't care much for more than one special character at the start or at the end, so I'd go with
/^[\s()+-]*(\d[\s()+-]?){6,}[\s()+-]*$/
This matches any number of special characters, followed by 6 or more times [a digit followed by at most one special character], followed by any number of special characters.
Another option would be to strip your special characters from the string first, and then match against 6 or more digits.
var rawInput = " 12 (3) -- 4 -5 ++6";
var strippedInput = rawInput.replace(/[\s()+-]*/g, "");
return new RegExp("^\d{6,}$").test(strippedInput);
Remember that you have a complete programming language at your disposal. I've noticed people tend to decide they need to use regex and then forget about everything else they can do.

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}$/

Regular Expression with optional elements in input string in javascript

Can anyone give me the regular expression for currency which have the following formats :
1000 - valid
1,000 - valid
1,000.00 or 1000.00 - valid.
This means, the number May or May Not contain a comma(,) separator every 3 digits.
The number May Or May Not contain a dot (.), and if it carries a dot(.) it should show atleast 1 number after the decimal place.
And lastly it should be numerical characters only. If I need to make my question clear kindly suggest.
/^\d{1,3}(?:(?:,\d{3})*|(?:\d{3})*)(?:\.\d{1,2})?$/
"Between one and three digits, then any number of groups of three digits prefixed by a comma or any number of groups of three digits not prefixed by said comma (disallowing a mix of the two kinds of groups), then an optional group of one or two digits prefixed by a dot."
Note: This regex assumes that you want to validate an entire string against the criteria outlined in your question. If you want to use it to find such numbers in a longer string, you will need to remove the ^ and $ from the beginning and end of the expression.
Something like so should work: (,?\d{3})+(\.\d{2})?. The regex will attempt to match a sequence of 3 digits precedeed by an optional comma, which is then, finally followed by an optional decimal point and 2 digits.
Please refer to this tutorial for more information.
EDIT: As per the comment below, the above regex can fail. I would recommend first using this regular expression: ^[\d][\d.,]+$ to make sure that you only have digits, thousand and decimal seperators. This regular expression will also make sure that the number starts with a digit, not with anything else. You could most likely have one regular expression which does everything, but it will most likely be quite complex.

Categories

Resources