AngularJS input to accept integers and special characters - javascript

<input type="text" ng-pattern="/^[0-9]*$/" id="players" name="players" ng-model="team.players"/>
I'd like the above input to accept integer numbers along with the following special characters.
. + -
How can I filter this with minimum code?
I tried some examples found on Internet, with none of them are working correctly whatsoever.
Note: My goal is to accept integer values (along with the special characters mentioned above) but not decimals.

Let us assume that want to accept the following numbers, which are all integral: 37, -10, -09, +23, 7., 7.000.
The following regex:
^[+\-]?[0-9]+(\.0*)?$
will accept an optional leading sign, one or many digits, and an optional decimal point with only trailing zeroes. It will deliberately not accept numbers without any digits before the decimal point, e.g. .00. The regex is anchored with ^ and $ to ensure that the whole string matches.
Depending on the regex dialect you may have to quote more or less characters.

I try regex, but it didn't work.
I end up with this code below,
$scope.reg.players=parseInt($scope.reg.players)
So when user put 1.2 it will convert to 1.
If user type 1. then it print invalid input error.

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

regex for specific phone number arrangement

i'm currently using this regex
"/^([0-9\(\)\/\+ \-]*)$/",
which is fine,but the problem is i am also using a masking script,
which produces this line automatically,
(___) ___-____
and it messes up my validation, what regex code can allow me to verify only this type of input from the use
(999) 999-9999
and also not accept a "blank" input field from user when entered. any length is fine, as long as it only accepts this inputs that i mentioned above.
This should work:
^\(\d{3}\)\s{0,1}\d{3}-\d{3}$
Breaking this regexp:
\(\d{3}\) matches only three numbers between brackets.
\s{0,1} matches only 0 or 1 space.
\d{3}-\d{3} matches only three numbers followed by '-' and then three other numbers.
First, when asking about regular expressions, you should always say which language or tool you are using because that affects what features are available and which characters need to be quoted with backslash. I'll assume you're asking about JavaScript based on your question's tags.
You say any length is fine. I shall take that to mean that each sequence of consecutive digits can contain any number of digits from one to infinity. I shall assume there's exactly one space and exactly one dash. On that basis, your RE is:
/^\(\d+\) \d+-\d+$/
If, as is more likely, you want to limit the lengths of the digit sequences, you would say something like:
/^\(\d{3,4}\) \d{3}-\d{1,10}$/
(three or four digits, exactly three digits, one to ten digits).
I have omitted any capturing parentheses (...) , which are a bit redundant if you're capturing the whole string ^(....)$ .
Here's a concise summary of JavaScript regex syntax:
http://www.regextester.com/jssyntax.html
Formatting and validation are two very different things. If you try to mix them, you will fail.
That being said, before performing validation you should strip all formatting characters from your string, then validate the content.
// remove everything that isn't a digit
var strippedNumber = value.replace(/\D/g, '');
if (strippedNumber.length === 10) {
// valid phone number
}

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.

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

Categories

Resources