Click here to Skip to main content
Sign Up to vote bad
good
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:57

Comments
Sergey Alexandrovich Kryukov - 19 Nov '12 - 0:38
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:41
Better allow the user to add any number of leading and trailing blank spaces, only, before validation, work with myComboBox.Text.Trim(). --SA

2 solutions

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:47
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:55
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.
 
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  
Comments
shaikh-adil - 19 Nov '12 - 7:02
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:56
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:44
nice one but i didint understood what you have done. but it is usefull
sariqkhan - 22 Nov '12 - 3:41
+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:29
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,356
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid