JSF telephone numbers regular expression - regex

I'm using primefaces of JSF, I'm doing a regular expression checking for the input of phone number, accepted values are:
909888
+90877845
here's my code
<p:inputText validatorMessage="invalid value">
<f:validateRegex pattern="^[+]?[0-9]+$" />
</p:inputText>
My problem is that the expression does not pass the empty values, as the field is not required

You are really close, you need to surround the entire match in a ()?. This means the regex will need to match the entire inside 1 or 0 times. A blank string will validate against the 0, and your +#### will validate on the time where that pattern is matched once.
^([+]?[\d]+)?$ should work, it also makes it a little easier to read since \d means any digit, but 0-9 should work fine. I did some testing with your values and the new addition here.
Also, if there is a specific count of numbers that can be in range (i.e. valid if the number is between 6 and 8 in length) you can modify the [\d]+ to [\d]{x,y} where x and y are your minimum/maximum length requirements.

Related

regular expression that accepts numbers like 1,000.10? [duplicate]

I need regex to validate a number that could contain thousand separators or decimals using javascript.
Max value being 9,999,999.99
Min value 0.01
Other valid values:
11,111
11.1
1,111.11
INVALID values:
1111
1111,11
,111
111,
I've searched all over with no joy.
/^\d{1,3}(,\d{3})*(\.\d+)?$/
About the minimum and maximum values... Well, I wouldn't do it with a regex, but you can add lookaheads at the beginning:
/^(?!0+\.00)(?=.{1,9}(\.|$))\d{1,3}(,\d{3})*(\.\d+)?$/
Note: this allows 0,999.00, so you may want to change it to:
/^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/
which would not allow a leading 0.
Edit:
Tests: http://jsfiddle.net/pKsYq/2/
((\d){1,3})+([,][\d]{3})*([.](\d)*)?
It worked on a few, but I'm still learning regex as well.
The logic should be 1-3 digits 0-1 times, 1 comma followed by 3 digits any number of times, and a single . followed by any number of digits 0-1 times
First, I want to point out that if you own the form the data is coming from, the best way to restrict the input is to use the proper form elements (aka, number field)
<input type="number" name="size" min="0.01" max="9,999,999.99" step="0.01">
Whether "," can be entered will be based on the browser, but the browser will always give you the value as an actual number. (Remember that all form data must be validated/sanitized server side as well. Never trust the client)
Second, I'd like to expand on the other answers to a more robust (platform independent)/modifiable regex.
You should surround the regex with ^ and $ to make sure you are matching against the whole number, not just a subset of it. ex ^<my_regex>$
The right side of the decimal is optional, so we can put it in an optional group (<regex>)?
Matching a literal period and than any chain of numbers is simply \.\d+
If you want to insist the last number after the decimal isn't a 0, you can use [1-9] for "a non-zero number" so \.\d+[1-9]
For the left side of the decimal, the leading number will be non-zero, or the number is zero. So ([1-9]<rest-of-number-regex>|0)
The first group of numbers will be 1-3 digits so [1-9]\d{0,2}
After that, we have to add digits in 3s so (,\d{3})*
Remember ? means optional, so to make the , optional is just (,?\d{3})*
Putting it all together
^([1-9]\d{0,2}(,?\d{3})*|0)(\.\d+[1-9])?$
Tezra's formula fails for '1.' or '1.0'. For my purposes, I allow leading and trailing zeros, as well as a leading + or - sign, like so:
^[-+]?((\d{1,3}(,\d{3})*)|(\d*))(\.|\.\d*)?$
In a recent project we needed to alter this version in order to meet international requirements.
This is what we used: ^-?(\d{1,3}(?<tt>\.|\,| ))((\d{3}\k<tt>)*(\d{3}(?!\k<tt>)[\.|\,]))?\d*$
Creating a named group (?<tt>\.|\,| ) allowed us to use the negative look ahead (?!\k<tt>)[\.|\,]) later to ensure the thousands separator and the decimal point are in fact different.
I have used below regrex for following retrictions -
^(?!0|\.00)[0-9]+(,\d{3})*(.[0-9]{0,2})$
Not allow 0 and .00.
','(thousand seperator) after 3 digits.
'.' (decimal upto 2 decimal places).

Regex to match number of digits in a phone string?

Trying to put together regex that can match minimum 4 digits, maximum 16 digits, and those digits can be separated by characters: ()- x+ (but should not be part of the min/max count).
ie. "555-123-4567" would return true, "1-234" is true, "+44(55)123-3333" is true, "abcd1" is false, "1-()-4++++-()-6" is false.
Any way to do that with purely regex? Trying a couple expressions but not working.
what you need to do, is to match any number of the allowed characters, followed by a digit, followed by any number of the allowed characters, and match that same sequence between 4 an 16 times.
like this
^([()\- x+]*\d[()\- x+]*){4,16}$
http://rubular.com/r/6VhALkFPQZ
This:
/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/
Works with these formats:
123-456-7890
(123) 456-7890
1234567890
123.456.7890
TL;DR
The OP has already accepted a regex solution. Below I present an alternative way of looking at the problem. Hopefully it helps the OP, but it's really aimed more at future visitors and followers of regex.
Don't Validate Logic with Regexps
Regular expressions work best for matching or extracting patterns, rather than for complex data validation. For example, the OP gives the following rules:
Trying to put together regex that can match minimum 4 digits, maximum 16 digits, and those digits can be separated by characters: ()- x+ (but should not be part of the min/max count).
but then says that 1-()-4++++4-()-66 should be false. However, it meets the rules for truth as originally defined by the OP. (NB: This example was later changed in the OP's question, but the point I'm making remains valid.)
Example: Using Code to Simplify the Regex Pattern Match
Logic should be encapsulated in short, testable pieces of code, not in complex regular expressions. For example, consider the following Ruby code:
numbers = [
'555-123-4567',
'1-234',
'+44(55)123-3333',
'abcd1',
'1-()-4++++4-()-66'
]
numbers.map { |num| num.delete '- x+()' }.grep /\A\d{4,16}\z/
#=> ["5551234567", "1234", "44551233333", "14466"]
Even if you aren't a Rubyist, the code should be easy to follow. This code strips out the characters that are irrelevant to our match, then checks that each string contains nothing but 4-16 digits anchored to the beginning and end of the string. Instead of validating a complex pattern, you're now just validating a simple pattern (e.g. all numbers) with a well-defined interval from 4 to 16. Furthermore, you can break this kind of logic up into smaller steps rather than simply calling long method chains, making this inherently more testable.
Example: Avoiding Regexp Validation Altogether
You could even go further by avoiding the regex for any sort of validation, and making your Boolean expressions more explicit. Consider the following:
numbers = [
'555-123-4567',
'1-234',
'+44(55)123-3333',
'abcd1',
'1-()-4++++4-()-66'
]
numbers.map do |num|
digits = num.scan /\d/
valid = digits.count >= 4 and digits.count <= 16
puts "#{num}: #{valid}"
end
This will print:
555-123-4567: true
1-234: true
+44(55)123-3333: true
abcd1: false
1-()-4++++4-()-66: true
To me, this seems like a much more robust and flexible way of solving the "phone number validation" question, which gets asked here on Stack Overflow in one form or another with amazing regularity. Your mileage may vary.

Parse Number from string with regex

I have thousands of article descriptions containing numbers.
they look like:
ca.2760h3x1000.5DIN345x1500e34
the resulting numbers should be:
2760
1000.5
1500
h3 or 3 shall not be a result of the parsing, since h3 is a tolerance only
same for e34
DIN345 is a norm an needs to be excluded (every number with a trailing DIN or BN)
My current REGEX is:
[^hHeE]([-+]?([0-9]+\.[0-9]+|[0-9]+))
This solves everything BUT the norm. How can I get this "DIN" and "BN" treated the same way as a single character ?
Thanx, TomE
Try using this regular expression:
(?<=x)[+-]?0*[0-9]+(?:\.[0-9]+)?|[+-]?0*[0-9]+(?:\.[0-9]+)?(?=h|e)
It looks like every number in your testcase you want to match exept the first number is starting with x.This is what the first part of the regex matches. (?<=x)[+-]?0*[0-9]+(?:\.[0-9]+)?The second part of the regex matches the number until h or e. [+-]?0*[0-9]+(?:\.[0-9]+)?(?=h|e)
The two parts [+-]?0*[0-9]+(?:\.[0-9]+)? in the regex is to match the number.
If we can assume that the numbers are always going to be four digits long, you can use the regex:
(\d{4}\.\d+|\d{4})
DEMO
Depending on the language you might need to replace \d with [0-9].

Regular expression for Less or more than 9 digits Repeating digits for first 5 or all 9 digits and only number?

I have this
"^(?!(11111|22222|33333|44444|55555|66666|77777|88888|99999|00000))([0-9]\d{8})"
regular expression in c# code and javascript works fine but in text tag of adobe echosign doesnt works anyone have anotherway to work on texttag of echosign?
By your last comment, you need a regex to validate a string of maximum length 9 containing digits only:
^[0-9]{1,9}$
This will validate any string containing digits, with length at least 1 and not greater than 9.
If you want to avoid strings such as 098 (leading zeroes), use this instead:
^[1-9][0-9]{0,8}$
EDIT: If I understand your question well now, you can use this regex:
^(?!([0-9])\1\1-\1\1)[0-9]{3}-[0-9]{2}-[0-9]{4}$
That is assuming that echosign can handle callbacks, if not, you can use this instead:
^(?!(?:111-11|222-22|333-33|444-44|555-55|666-66|777-77|888-88|999-99|000-00))[0-9]{3}-[0-9]{2}-[0-9]{4}$

Is there a regex to match a numeric range, and also ensure that it's a valid range?

Is there a regular expression to match a numeric range, e.g. 1 - 20?
If so, is it possible to ensure that the left value is always less than the right value? It wouldn't make sense to have a range e.g. 20 - 1 or 15 - 5
As the commentors note: if this is possible in a regex it will be very hard. There is no direct support to perform arithmetical comparisons (including greater than) in a regex.
Better to use a regex to validate the format and capture the two numbers. If the regex matches then use the host language to convert the captures into numbers and compare.
Yes you can do that. You can ensure that a given number is smaller than another number in the same text.
This regex tests whether the first number is smaller than the next number: Format: XXX,YYY
XXX < YYY:
\b(?:[1-9](?<open>\B\d)+\d*,(?<close-open>\d)+(?(open)(?!))\b
|
(?<prefix>\d*)(?:(?<g0>0)|(?<g1>1)|(?<g2>2)|(?<g3>3)|(?<g4>4)|(?<g5>5)|(?<g6>6)|(?<g7>7)|(?<g8>8)|(?<g9>9))(?<suffix>\d)*,\k<prefix>(?(g0)(?!)|(?(g1)0|(?(g2)[01]|(?(g3)[0-2]|(?(g4)[0-3]|(?(g5)[0-4]|(?(g6)[0-5]|(?(g7)[0-6]|(?(g8)[0-7]|(?(g9)[0-8]))))))))))(?<suffix2-suffix>\d)*(?(suffix)(?!)))\b
XXX > YYY:
(?<open>\B\d|\b[1-9])+,[1-9](?<close-open>\d)+(?(open)(?!))\d*\b
|
(?<prefix>\d*)(?:(?<g0>0)|(?<g1>1)|(?<g2>2)|(?<g3>3)|(?<g4>4)|(?<g5>5)|(?<g6>6)|(?<g7>7)|(?<g8>8)|(?<g9>9))(?<suffix>\d)*,\k<prefix>(?(g0)[1-9]|(?(g1)[2-9]|(?(g2)[3-9]|(?(g3)[4-9]|(?(g4)[5-9]|(?(g5)[6-9]|(?(g6)[7-9]|(?(g7)[89]|(?(g8)9|(?(g9)(?!)))))))))))(?<suffix2-suffix>\d)*(?(suffix)(?!))
If you want to use - as separator you only have to replace the , with - in this regex. This regex was created and tested using C# regex.