Click here to Skip to main content
12,396,461 members (34,381 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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?
public bool valcombo()
        {
            if (comboBox1.Text == string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
Posted 18-Nov-12 17:57pm
Comments
   
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
   
Better allow the user to add any number of leading and trailing blank spaces, only, before validation, work with myComboBox.Text.Trim().
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You can try this:


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;
   }
}
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You can simply use this.

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

  Permalink  
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
   
+5
but 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2016
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