nryan30
asked on
C# checking input value
I have a text box as an input field...
I basically want to check if the input is a 7 digit number or a 7 digit number such as this 333.4444. Separated by a dot.
Is there a way to do this in the code behind?
I basically want to check if the input is a 7 digit number or a 7 digit number such as this 333.4444. Separated by a dot.
Is there a way to do this in the code behind?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
string test = "333.4444";
int counter = 0;
for(int i=0; i<test.Length; i++)
{
if(char.IsNumber(test,i)) counter++;
}
if(counter == 7)
{
MessageBox.Show("Test Successfull.");
}
else
{
MessageBox.Show("Test failed.");
}
int counter = 0;
for(int i=0; i<test.Length; i++)
{
if(char.IsNumber(test,i)) counter++;
}
if(counter == 7)
{
MessageBox.Show("Test Successfull.");
}
else
{
MessageBox.Show("Test failed.");
}
Just an addition to jatinderalagh's solution.
also check for IsNumber for the whole number.
Otherwise "333A4444" will also show "Test Successfull"
also check for IsNumber for the whole number.
Otherwise "333A4444" will also show "Test Successfull"
Would 3333.444 be okay? How about 33333.44?
@jatinderalagh:
Does IsNumber return true or false for decimal '.'.
Is this an international application? Maybe the decimal is a comma ','.
Bob
@jatinderalagh:
Does IsNumber return true or false for decimal '.'.
Is this an international application? Maybe the decimal is a comma ','.
Bob
@Bob
[C#] public static bool IsNumber(char);
Indicates whether the specified Unicode character is categorized as a number.
Supported by the .NET Compact Framework.
[C#] public static bool IsNumber(string, int);
Indicates whether the character at the specified position in a specified string is categorized as a number.
Supported by the .NET Compact Framework.
As it supports the unicode characters. So it does not matter whether it's a international appplication or not.
Jatinder
[C#] public static bool IsNumber(char);
Indicates whether the specified Unicode character is categorized as a number.
Supported by the .NET Compact Framework.
[C#] public static bool IsNumber(string, int);
Indicates whether the character at the specified position in a specified string is categorized as a number.
Supported by the .NET Compact Framework.
As it supports the unicode characters. So it does not matter whether it's a international appplication or not.
Jatinder
What I am trying to ask is:
What is the result of IsNumber(".") or IsNumber(",")? Because you are not looking at the string as a whole, but each individual character.
Bob
What is the result of IsNumber(".") or IsNumber(",")? Because you are not looking at the string as a whole, but each individual character.
Bob
false
This should return false for 333A4444, but since it only counts for 7 numeric values, then this would be seen as valid (from puranik_p comment).
@puranik_p:
How would you check IsNumber with the entire string? It is char.IsNumber, not string.IsNumber.
Code modificatiion from jatinderalagh's code (not for points):
string test = "333A4444";
bool valid = true;
int counter = 0;
char dec = System.Globalization.Numbe rFormatInf o.CurrentI nfo.Number DecimalSep arator.ToC harArray() [0];
foreach (char ch in test.ToCharArray())
{
bool numeric = char.IsNumber(ch);
if (ch != dec)
if (!numeric)
{
valid = false;
break;
}
else if (numeric)
counter +=1;
}
if (valid && counter == 7)
MessageBox.Show("Test successful.");
else
MessageBox.Show("Test failed.");
Bob
@puranik_p:
How would you check IsNumber with the entire string? It is char.IsNumber, not string.IsNumber.
Code modificatiion from jatinderalagh's code (not for points):
string test = "333A4444";
bool valid = true;
int counter = 0;
char dec = System.Globalization.Numbe
foreach (char ch in test.ToCharArray())
{
bool numeric = char.IsNumber(ch);
if (ch != dec)
if (!numeric)
{
valid = false;
break;
}
else if (numeric)
counter +=1;
}
if (valid && counter == 7)
MessageBox.Show("Test successful.");
else
MessageBox.Show("Test failed.");
Bob
Actually...I think this expression would work a little better.
"[0-9]{3}[.][0-9]{4}"
"[0-9]{3}[.][0-9]{4}"
@Bob,
>It is char.IsNumber, not string.IsNumber
I was thinking about IsNumeric (VB) when I posted this.
Thanks for correcting.
Pura
>It is char.IsNumber, not string.IsNumber
I was thinking about IsNumeric (VB) when I posted this.
Thanks for correcting.
Pura
Otherwise you could use a regular expression.