i need to have a regular expression which accept atleast 1 digit number and maximum 5 digit number and if user enter zero in the following fashion like '00','000','0000','00000' then expression should reject such inputs.
currently, i am using ^[0-9]{1,5}$.
If you'd make sure that the user's input is formatted as a 5 digit number with leading zeroes, then the following regex would work:
^[0-9]{5}(?<!00000)$
This uses negative lookbehind to ensure that the string entered was not 5 zeroes.
^(?=.*[1-9].*)[0-9]{1,5}$
Uses a lookahead assertion to make sure there's at least one nonzero digit. If there is one, then the rest of the expression only matches if there's between 1 and 5 digits. Both conditions have to be met, or the expression won't match.
Agreed, though, that if you're trying to match a number, as opposed to a string of digits (like a ZIP code), it'd probably be better to compare numerically rather than with a regex.
Not nice but working:
from 1 to 99999: [1-9][0-9]{0,4}
from 01 to 09999: 0[1-9][0-9]{0,3}
from 001 to 00999: 00[1-9][0-9]{0,2}
from 0001 to 00099: 000[1-9][0-9]{0,1}
from 00001 to 00009: 0000[1-9]
And then putting all this together:
^(0[1-9][0-9]{0,3}|00[1-9][0-9]{0,2}|000[1-9][0-9]{0,1}|0000[1-9]|[1-9][0-9]{0,4})$
Edit: updated.
Does it really need to be a regular expression? Why not just check if 0 < value && value <=99999?
Ok I understand, here the solution :
^[1-9][0-9]{0,4}$
Take all number between 1 and 99999
Why don't you use a RangeValidator it is simplier.
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Value must be greater than zero." ControlToValidate="tbQty" MinimumValue="1" MaximumValue="99999" Type="Integer"></asp:RangeValidator>
Related
I am going through the documnetation of ASP.NET on Regular Expressions where I stuck at following expression
^[0-9]{5}$
The Input (Mathches) are, 11111, 12345, 55555
Now, from my understanding , first and third are correct (First character can be 0-9 and there must be five occurrences of that character). Please make me understand how second is a valid match.
The {5} means that the match must be repeated, and [0-9] matches any digit. So this matches any 5 digits, not especially 5 identical digits.
My textbox should allow "The first character should be alphabetic or numbers and the remaining characters only numbers"
ex: #999999999 here # represent alphabet(a-z,A-Z) or numbers(0-9),
Please, help me.
I thought you need something like this:
in case you need 0 or more numbers after first symbol
[a-zA-Z0-9][0-9]*
in case you need 1 or more numbers after first symbol
[a-zA-Z0-9][0-9]+
I've been working on a project with lots of previous developer validation issues and am trying to clean up some issues and add validation in spots.
I generally use this simple regex that allows only positive whole values and can accept commas if they are entered:
"^([0-9]*,?)*$"
But in this case my program won't run if 0 is the value entered, so I need 1 to be the minimum value allowed. All else is the same. No decimals or any of that stuff.
I'm assuming I just need to add "not 0" to my regex, but not sure how to do that.
Allowed:
3,200
650
5
134560
100,000
Not Allowed:
0
3.2
-3.2
Thanks for your help!
Simply use this:
^[1-9]([0-9]*,?)*$
The first character cannot be a 0 and since it isn't optional either...
I need regular expression for phone number.
(from 0 to 9) (total 10 digits)
Example: "0123456789"
Normally a \d{10} would do the trick. But that is the lazy-man's way of validating as '0000000000' would pass a a valid entry.
But in case you know more about the domain, and the rules (I mean, if your phone number belongs to a specific country) and you want to make sure the number matches the local rules, then you can be a little bit more specif.
For example if all the numbers are starting with a leading zero, you can do this
0\d{9}
Or if the prefixes are well know... you can make an expression that allows phone numbers only starting with certain prefix(es).
(017|016|019|012)\d{7}
This will allow only those prefixes in the list, plus other 7 digits.
Use this pattern and match it:
\d{10}
\d : digits only.
{n}: numbers of occurrences.
You can refer this post for more info. Regex for Phone number
its simple:
\d{10}
\d allows digits, and {10} indicates that the phone number must be exactly 10 digits long.
As you mentioned in the comment that you also want a regex for 012-345-6789, (\d{3}-){2}\d{4} would do the work.
(\d{3}-) would validate the first 3 digits and a -
(\d{3}-){2} would look for two occurrence of the above described group. So will validate: 012-345- And at last \d{4} will look for the last 4 digits
i have a validation in my .net textbox where it will take only numbers
but when i put the the phone format like
080 234234
it will not accept because of a space
how to resolve this ?
could anyone help in regular expression ?
Current expression is this [0-9]+
Simply add space to characters range:
[0-9][0-9 ]*
You can also add start and stop indicators:
^[0-9][0-9 ]*$
EDIT:
number must start with digit followed with digits or spaces (zero or more).
You could use
([0-9]+\s*)+
or
(\d+\s*)+
either of which would allow one or more groups of digits followed by optional whitespace
Really, the best way to deal with this is to remove all non-digit characters, then do whatever additional validation you may require, such as the number of digits or whether the number begins with a valid area code/country code, on what's left. That way it doesn't matter whether the number is entered as (assuming US numbers here) 987-654-3210, (987) 654-3210, 987 654 3210, 9876543210, 9 8 7-6.54321 0, or whatever else.
Concentrate on validating what's meaningful in the input (the digits) and not incidental details which really don't matter (how the digits are grouped or formatted).