Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
i have used this method for required field combo box but the problem is. i want to validate combo box with spaces. i dont want user to enter spaces at the start and end of string and i dont want user to enter more than one whitespaces one after the other like adil shaikh how can i achive this?
C#
public bool valcombo()
        {
            if (comboBox1.Text == string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Posted
Comments
Sergey Alexandrovich Kryukov 19-Nov-12 0:38am    
The statement like

if (something) { return true; } else { return false; }

shows deep confusion about "if" and condition, and, basically, on how to do simple programming. It should be:

return something; //instead!

--SA
Sergey Alexandrovich Kryukov 19-Nov-12 0:41am    
Better allow the user to add any number of leading and trailing blank spaces, only, before validation, work with myComboBox.Text.Trim().
--SA

You can try this:


C#
public bool valcombo()
{
   //It will prevent space in begin, End and double space within the text
   if (comboBox1.Text.StartsWith(" ")|| comboBox1.Text.EndsWith(" ")|| comboBox1.Text.Contains("  "))
   {
       return true;
   }
   else
   {
       return false;
   }
}
 
Share this answer
 
Comments
Shmuel Zang 19-Nov-12 0:47am    
At first thought, I thought about a Regex. But, your solution is more suited to this case. - My 5.
shaikh-adil 19-Nov-12 0:55am    
i had used regex for that.but whitespace regex is too difficult to develop.
but this answer was awsome.
+5
thank you sir
You can simply use this.

C#
private Boolean valcombo(){
if ((comboBox1.Text.Length - comboBox1.Text.Trim().Length > 0) ||      

(comboBox1.Text.Trim().Length - comboBox1.Text.Trim().Replace(" ", "").Length > 1)) 
            
return true;
            
else
                
return false;    
        }


input               output

" adil shaikh"      true
"adil shaikh "      true
"adil   shaikh"      true
"adil shaikh"      false
 
Share this answer
 
v3
Comments
shaikh-adil 19-Nov-12 7:02am    
you diditn understood my question.
I asked for validation for combo box not the textbox.
So where is the textbox1 and textbox2 comes???
deepak.m.shrma 19-Nov-12 23:56pm    
i updated solution now please check. its works same yaar... here its more imp how to deal with string not the element. its works for all element which deals with string :-) hope you will get my point.
shaikh-adil 22-Nov-12 3:44am    
nice one but i didint understood what you have done. but it is usefull
sariqkhan 22-Nov-12 3:41am    
+5but the what does this means?
comboBox1.Text.Length - comboBox1.Text.Trim().Length > 0)
(comboBox1.Text.Trim().Length - comboBox1.Text.Trim().Replace(" ", "").Length > 1))
deepak.m.shrma 22-Nov-12 5:29am    
actually i do not use substring or contain method. i actually use .length property of the string. here in ist lin.
ie.
(comboBox1.Text.Length - comboBox1.Text.Trim().Length > 0)
i checked. whether textbox contain space start and ends of the text.
if text original length - removing start or end space > 0 its mean it contain space either start of the text or at end.

in 2nd line or check.
ie.
comboBox1.Text.Trim().Length - comboBox1.Text.Trim().Replace(" ", "").Length > 1)

i checked that text does not contain more than 2 space between its name and sir name.
simple.

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