Regex accepts only numeric values. The first character cannot be 0

I have a text box that I created using .NET.

Using this text box, the user can only enter a numerical value. But don't start at 0. start at 1-9. after entering the user key in 1-9 on the first character, the user can enter 0.

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

This is my regex expression. Using this, I cannot enter 1-9. but only 0 and the alphabet. But if I use ^ [1-9], I can enter a numerical value, but I can not enter 0.

I have already tried the whole answer that you all offer. But it still can’t work. This is not like I am not reading your entire answer.

here is a photograph.

enter image description here

I want to check for the first time, only the user can enter a numerical value, but start with a value that is not equal to 0 or the alphabet, but 1-9. After the first character, the user can enter only 0-9.

I use Int.TryParse, but I use this when I click the button to process.

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

that the regular expression takes only a numerical value from 0 to 9.

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

so that the regular expression accepts only a number from 1 to 9.

How to add a larger regex expression for the second character until the rest accepts numeric 0-9?

By the way, I don't care about 0.99 because the price is fixed here. not with 0.99 or 0.123 ..

Any other ways to do this? Thank you

+6
source share
4 answers

If you are looking at something like price, you need to think that 0.99 is probably quite right. For something like this, I would just start with the non-profit ^[0-9]*(\.[0-9]{0,2})?$ (Again, there may be cross cases that can make it more complicated, like three digits after a decimal point, etc.) And allow the leading zeros, t "damage" the value anyway.

It should start from scratch, just change the initial [0-9]* to [1-9][0-9]* . Only for integers (as seen from your added sample data):

 ^[1-9][0-9]*$ 
+13
source

To combine a number starting with any digit, but zero:

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

And if you want to also combine 0:

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

remove the last plus if you only need one zero

To allow any alpha digit after the first non-zero:

 ^[1-9a-zA-Z][0-9a-zA-Z]*$ 
+8
source

Like your .NET code, you should not use a regular expression to parse the whole . Just use the 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 regex will do it ^[1-9]\d*$

+1
source

Sometimes regex doesn't even work in certain browsers. Therefore, there 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); } } 
0
source

All Articles