41

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.

Alfred Angkasa
  • 1,381
  • 3
  • 17
  • 35

7 Answers7

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]*$
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
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]*$
perreal
  • 94,503
  • 21
  • 155
  • 181
3

This is what worked the best for me:

^(?!(0))[0-9]+$
Jerlam
  • 943
  • 2
  • 12
  • 31
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*$

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
0

Here is what you want:

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

Which also accepts decimals like these numbers:

423423.423423

0.4234

0
Mahyar Ekramian
  • 191
  • 2
  • 5
0

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]*$
Dremiq
  • 335
  • 3
  • 10
-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);
}
} 
Shreyansh Patni
  • 401
  • 2
  • 17