I having a textbox which user need to enter customer number(ex : 123456789). what is my issue is if they enter (ex:000000000 or 000 or,0000,or 00000) I need to valid as it is ID Should not be zero invalid . how I need to do in C# code behind .
Asked
Active
Viewed 156 times
-2
-
you can check the textbox value and make sure it is different from 0 – Redaa Aug 01 '14 at 10:39
-
1Why don't you simply check `value>0` ?? – huMpty duMpty Aug 01 '14 at 10:39
1 Answers
1
I need to valid as it is ID Should not be zero invalid
If this is the only rule (I can't say like it), you can try to parse your string
(which is probably you get this from user) with Int32.TryParse
method and then check your integer is bigger than 0
or not. Like;
string input;
int number;
if (Int32.TryParse(input, out number))
{
if(number > 0)
{
// Your input can be parsed to int and it is greater than zero.
}
}
Remember, don't check your number is 0
or not. Because if your string couldn't parse with Int32.TryParse
method, your number will be 0
by default.
result Type: System.Int32
When this method returns, contains the 32-bit signed integer value equivalent of the number contained in s, if the conversion succeeded, or zero if the conversion failed.

Soner Gönül
- 97,193
- 102
- 206
- 364