Regex accept numeric only. First character can't be 0
Asked Answered
W

8

42

I have a textbox that I created using .NET..

By using that textbox, the user only can key in numeric. But not start with 0. start with 1-9. after the user key in the 1-9 at the first character, the user can key in 0.

Regex reg = null;
reg = new System.Text.RegularExpressions.Regex("^[1-9][0-9]*$")
return reg.IsMatch(str);

That is my regex expression. By using that i can't key in 1-9. but only 0 and alphabet. But if i using ^[1-9] I can key in numeric, but can't key in the 0.

I already tried all the answer that all of you suggest. But still can't work. It's not like i dont read all of your answer.

here is the picture..

enter image description here

I want to validate at the first time, the user only can key in numeric, but start with value that is not 0 or alphabet but 1-9. After the first character, user may only key in 0-9.

i do use Int.TryParse, but i use that after i hit a button to process.

reg = new System.Text.RegularExpressions.Regex("^[^0-9]");

that regex accept only numeric from 0 to 9.

reg = new System.Text.RegularExpressions.Regex("^[^1-9]");

that regex accept only numeric from 1 to 9.

How can i add more expression to regex for the second character until the rest that only accept numeric 0-9?

By the way, i don't care about 0.99 because in here, the price is fix. not with 0.99 or 0.123..

Any others way to do it? thanks.

Waterside answered 3/1, 2013 at 2:51 Comment(11)
Any regex to start with? Or is ^($|[^0]) an acceptable solution?Blackmon
for example i have to key in price.. user can't key in the price start with 0. they have to key in value between 1-9 at the first character, for the second character until the rest 0-9 is allowed.. do you know what is the regex syntax? thanks.Waterside
Again, should hello be accepted? It doesn't start with a zero.Blackmon
@JanDvorak - rejected. only numeric.Waterside
You did not say "only numeric" in the original question.Blackmon
Alfred, keep in mind that 0.99 is a perfectly valid (and heavily used, due to app stores) price.Dulcie
Hi paxdiablo, thanks for your information. But this is a textbox. i want to validate user can't key in a numeric that start with 0, but start with 1-9. after the first character then they can key in 0-9. any idea how to doint this?Waterside
There are already 3 good answer Alfred. Why don't you read them?Bifid
Since I seriously doubt I will ever know for sure what the asker actually wants, I vote to close as "not a real question".Blackmon
You could have used Int.TryParse hereBifid
Thats mean user can key in a value that start wih 0?Waterside
D
55

If you're looking at something like a price, you need to consider that 0.99 is probably perfectly valid. For something like that, I would simply start with a non-complex ^[0-9]*(\.[0-9]{0,2})?$ (again there may be edge cases that may make it more complex like three digits after the decimal point and so on) and allow leading zeroes, since they don't "damage" the value in anyway.

It it must start with a non zero, just change the initial [0-9]* to a [1-9][0-9]*. For integers only (as seems to be indicated by your added sample data), that would be:

^[1-9][0-9]*$
Dulcie answered 3/1, 2013 at 2:53 Comment(0)
C
26

To match a number starting with any digit but zero:

^[1-9][0-9]*$

And if you want to match 0 as well:

^([1-9][0-9]*)|([0]+)$

remove the last plus if you want a single zero only

To allow any alpha-numeric after first non-zero:

^[1-9a-zA-Z][0-9a-zA-Z]*$
Cyb answered 3/1, 2013 at 2:55 Comment(11)
Normally, a lone zero is desired to be matched as well.Blackmon
depending on the language, they might be non-capturing groups as I intended.Cyb
@JanDvorak when did op say lone zero to be matched as well?Bifid
@shiplu.mokadd.im never did he (yet). He didn't say anything about rejecting letters either.Blackmon
@JanDvorak but perreal has changed his answer for your uncertain assumption.Bifid
@Cyb he didn't say numeric in the first revision.Blackmon
try this before. 0 and alphabet is allowed. but i can't key in 1-9.Waterside
@AlfredAngkasa "alphabet is allowed" -- WTH? You want letters to be accepted? This will not accept letters.Blackmon
@AlfredAngkasa Alphabet is A-Z,a-z. digit is 0-9.Bifid
yeah i know..but here still can allowed to key in alphabet. I only want numeric but not start with 0.. i already try ^[1-9][0-9]*$.. still can allowed. why should i lie to all of you that this can still allowed?Waterside
@AlfredAngkasa, perhaps you have a problem with the language/tool you are using and not with the regex. The thing should not allow anything that your regex does not match.Cyb
V
4

This is what worked the best for me:

^(?!(0))[0-9]+$
Valuation answered 21/9, 2021 at 9:24 Comment(2)
This only works for single character numbers. like 1,2,3 but not 100, 200, 300 etc.Elman
Thanks, is use this (?!0)\d+Pediatrics
B
1

As your code is .NET you should not use regex to parse an Integer. Just use UInt32.TryParse() method

uint num=0;
if(UInt32.TryParse(str, out num)){
    Console.WriteLine("Converted '{0}' to {1}.", str, num);   
}else{
    Console.WriteLine("conversion of '{0}' failed.", value==null? "": value);
}

Old answer

This simple regular expression will do it ^[1-9]\d*$

Bifid answered 3/1, 2013 at 2:53 Comment(6)
What about the empty string? This will reject the empty string.Blackmon
OP didn't say anything about it.Bifid
But how about if user key in 0123556? Because the user can't key in 0 at he first character. How can i handle this? Thanks..Waterside
What is the problem with 0? its still a valid number. It'll be converted to 123556 after you use TryParseBifid
Hi, i just upload a picture for more clearly. For the first time, whenever user key in the value at the textbox, the user can only key in all numeric but not start with 0. If i key in 0123456 after i hit button, i do convert using Int.TryParse..Waterside
I think your code has other problem. ^[1-9]\d*$ is enough for this. IT'll match 1, 12, 10. In fact at least one digit but leading digit can not be 0.Bifid
A
1

This regex accepts numbers 0-9, doesn't allow 0 to be the first character and also allows the value to be "", this is useful for creating `fake` number inputs.

^(?!0)[0-9]*$
Accident answered 19/4, 2023 at 16:15 Comment(0)
L
0

Here is what you want:

^(([1-9]\d*)|([0]{1}))(\.\d+)?$

Which also accepts decimals like these numbers:

423423.423423

0.4234

0
Leery answered 1/9, 2021 at 13:13 Comment(0)
U
0

Input does not start with a zero, you can modify the regular expression to exclude the case where the input starts with zero. Here's an updated regex pattern:

Regex:- ^(?!0)[0-9]*$
Unplaced answered 4/1 at 11:50 Comment(0)
D
-1

Sometimes the regex wont even work in certain browsers. Therefore here is a similar logic:

function name(e)
{

var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
var reg = /\d/;

var textField=document.getElementById("ID").value;

if(key==48)
{
if(textField==""||textField==null)
{
e.preventDefault();
return false;
}

else{
return true;
}
}

else
{
var reg = /\d/;
return reg.test(keychar);
}
} 
Dogooder answered 6/4, 2016 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.