Click here to Skip to main content
12,750,464 members (37,334 online)
Rate this:
Please Sign up or sign in to vote.
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 28-Sep-12 18:55pm
Sergey Alexandrovich Kryukov 28-Sep-12 23:59pm
This is not really productive question. Why guessing what some unknown code does?
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

this code is for validating textbox mean that if textbox is empty or textbox does not contain number then it will return true.
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;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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 .
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? 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?? 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? 29-Sep-12 1:30am
Removing it will not change anything ? I am not understanding please tell me ?
sariqkhan 29-Sep-12 1:47am
okay nothing.i got it. and whtat does this line means
public void button1_Click(object sender, EventArgs e)
bool vName = ValidateNames();
bool vNumber = ValidateNumbers();
if (!vName && !vNumber) // esspecially this means what??
} 29-Sep-12 2:29am
u r calling two validation functions and if validation is success then your if part will executed otherwise else part will be executed.
sariqkhan 29-Sep-12 2:58am
validatenames() returns true or false
validatenumbers() returns true or false
then it means. Validatenames and validateNumber will return to false. Then if statement will execute.
Otherwise else statement will execute 29-Sep-12 3:34am
Yes .
sariqkhan 29-Sep-12 4:30am
thanks bro. Can you suggest such type of code. Which will return true or false if space is given in textbox. Means space is not allowed. or only varchar is allowed not the symbols not allowed.
Please can you help me in this 30-Sep-12 2:07am
firstly add name space

using System.Text.RegularExpressions;

protected bool fnValidateText()
bool flag= true;
Regex regCharacters= new Regex("^[a-zA-Z]*$");
return flag;//it will return false when validation failed
return flag;

sariqkhan 30-Sep-12 3:16am
thanks bro.
thanks to you. 30-Sep-12 4:10am
Your welcome .
sariqkhan 1-Oct-12 2:18am
bro can you help me out
My friend is making windows form and he needs username
which will be minimum of 4 charecters and maximum of 15 chracters
which will allow hifen and underscore and dot in the middle but not in starting and ending of username
(which will not allow symbols except hifen and underscore and dot)
should disallow below patters patters
aquib_ // means no symbols will be there at end
and username should not be numbers it should be mixing of numbers and alphabets or it should be only alphabets
hope this will be understood
i have got this regex
but it is not too usefull in his case. He posted more that 3 times here but some people are quite buzy i think.
Can you help me out
(i have downloaded expresso but i am not convenient with that environmen its too difficult to undestand 1-Oct-12 5:07am
Try this :
Regex regValid = new Regex("^[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]*$|_[a-zA-Z0-9]*$|.[a-zA-Z0-9]*$)?$");

It will allow like that :


but should set MaxLength of is not checking minimum length of text .
On button click u can check minimum length of text .

I hope this will help u.

sariqkhan 1-Oct-12 5:23am
nice bro its truly hard.
how you have developed? 1-Oct-12 5:26am
yes bro,after getting your message i tried for it i think it took around more two hours .
sariqkhan 1-Oct-12 5:30am
: |
but in two hours you are in loss
because the regex is accepting
this typ of example
sorry bro i wasted your presious time
really 1-Oct-12 5:42am
This not wastage of time ,this is learn something new .wastage time means simply sitting and waiting for ready made solution .it is write is not 100% accurate but by using your brain u can modify it .
sariqkhan 1-Oct-12 7:18am
^(?:[a-zA-Z0-9](?:\.|_|-)?)+[a-zA-Z0-9]$ bro i got this one but it doesnt support charecter restriction it takes any number of charecters so how to give the whole string a restrictive charecters , means the string generated by regex should be not more that 15 charecters? it is possible? 1-Oct-12 7:26am
have checked code for only digits like :123455 it accepting only digits
for string length i told set MaxLength property of textbox .
sariqkhan 1-Oct-12 7:44am
oh.. i forgot to check.
can you remove this fault of numerics?
please bro i am in need
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


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 :
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;
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100