Click here to Skip to main content
Rate this: bad
good
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
sariqkhan1.4K
Comments
Sergey Alexandrovich Kryukov at 28-Sep-12 23:59pm
   
This is not really productive question. Why guessing what some unknown code does?
--SA
sariqkhan at 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
good
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.
  Permalink  
Comments
sariqkhan at 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
good
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 .
  Permalink  
Comments
sariqkhan at 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 at 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 at 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 at 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 at 29-Sep-12 1:22am
   
means i am initializing the stat value to false.? Removing it will not change anything?
solanki.net at 29-Sep-12 1:30am
   
Removing it will not change anything ? I am not understanding please tell me ?
sariqkhan at 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??
{
MessageBox.Show("sucess");
}
else
{
MessageBox.Show("error");
}
solanki.net at 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 at 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
solanki.net at 29-Sep-12 3:34am
   
Yes .
sariqkhan at 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
solanki.net at 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]*$");
if(!regCharacters.IsMatch(textbox1.text))
{
flag=false;
return flag;//it will return false when validation failed
}
return flag;
}
 
sariqkhan at 30-Sep-12 3:16am
   
thanks bro.
thanks to you.
solanki.net at 30-Sep-12 4:10am
   
Your welcome .
sariqkhan at 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
_aquib
.aquibxyz
aquib.
aquibxyz--qureshi
aquib__xyzqureshi
aquibqureshi-
aquib..qureshi
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
^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-]){0,4}[a-zA-Z0-9])$
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
solanki.net at 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 :
 
test.99
te33.ttss33
test_ttssee3
-----------
 
but should set MaxLength of text.it is not checking minimum length of text .
On button click u can check minimum length of text .
 
I hope this will help u.
 

 

sariqkhan at 1-Oct-12 5:23am
   
nice bro its truly hard.
how you have developed?
solanki.net at 1-Oct-12 5:26am
   
yes bro,after getting your message i tried for it i think it took around more two hours .
sariqkhan at 1-Oct-12 5:30am
   
omg
: |
but in two hours you are in loss
:(
because the regex is accepting
this typ of example
aquib+aquib
solanki=solanki
sorry bro i wasted your presious time
really
solanki.net at 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 at 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?
solanki.net at 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 at 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
good
Please Sign up or sign in to vote.

Solution 3

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 :
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;
  Permalink  
Comments
sariqkhan at 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
0 OriginalGriff 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
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