Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
C#
private bool ValidateNumbers()
        {
            if (textBox2.Text == string.Empty)
            {
                return true;
            }
            char[] testArr = textBox2.Text.ToArray();
            bool stat = false;
            for (int i = 0; i < testArr.Length; i++)
            {
                if (!char.IsNumber(testArr[i]))
                {
                    stat = true;
                }
            }
            return stat;
        }


i am developing a validation control using a method and validatenumber
found this piece of code on net. but i dont know the logic behind this one
Posted
Comments
Sergey Alexandrovich Kryukov 28-Sep-12 23:59pm    
This is not really productive question. Why guessing what some unknown code does?
--SA
sariqkhan 29-Sep-12 0:08am    
no. its not unknown code. i have implemented it in my project and i am a learner.
i have to present what i have implemented.
and by learning i can make new such type of code. especially i am learning because i have to explain what i had implemented.
can you help me in this

this code is for validating textbox mean that if textbox is empty or textbox does not contain number then it will return true.
 
Share this answer
 
Comments
sariqkhan 29-Sep-12 0:12am    
can you explain me this part in detail please
.
char[] testArr = textBox2.Text.ToArray();
bool stat = false;
for (int i = 0; i < testArr.Length; i++)
{
if (!char.IsNumber(testArr[i]))
{
stat = true;
}
}
return stat;
firstly it storing your textbox value in character array .
then its checking value character by character if it is not a number then it will set stat=true.
ex -u enter '123s' in your texbox then it will return stat=true due to last character .
use break point then u can unerstand easily .
 
Share this answer
 
Comments
sariqkhan 29-Sep-12 0:46am    
i dont know how to use breakpoint
but bro this code is for checking that there is any number in that textbox?
then whats your meaning
"it will return stat=true due to last character ."
why the hell we are concentrating about chat? this code is for checking numbers? right?
solanki.net 29-Sep-12 0:59am    
this code is not for checking there is any number in textbox .this is for checking only number should be in textbox .if textbox contain any thing except the number then it will return stat= true.
sariqkhan 29-Sep-12 1:05am    
okay buddy you are right and what does this means
char[] testArr = textBox2.Text.ToArray();
bool stat = false; // what does this line do??
solanki.net 29-Sep-12 1:17am    
it means that when your textbox contain only number then it will return stat=false;because stat default value is false and when textbox contain only number then it will not change .
sariqkhan 29-Sep-12 1:22am    
means i am initializing the stat value to false.? Removing it will not change anything?
Hi,

What I guess about your needed validation is :
return true if the text is either blank or numeric string. (i.e. if the text is entered then it should be numeric. Blanks are allowed.)

so in above function once change will be required :
C#
char[] testArr = textBox2.Text.ToArray(); 
bool stat = false; 

for (int i = 0; i < testArr.Length; i++) 
{ 
 if (!char.IsNumber(testArr[i])) 
   {
    return false;  // when any non digit character is found then no need to check 
                   //any more characters in the string.
   }
 } 
}


return true;
 
Share this answer
 
Comments
sariqkhan 29-Sep-12 1:01am    
error is coming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900