I have a tex box in aspx page and i need to validate the text box. How can i do this at server side.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
if(Textbox1.Text=="")
{
lblError.Text ="Enter required field":
}
this is working when user leaves the text box blank. But when he enter white spaces the message is not shown. Is there any solution to this?
If your using .net 4.0 or above than following work.
if(string.IsNullOrWhiteSpace(Textbox1.Text))
{
lblError.Text ="Enter required field";
}
otherwise you have to check manually...
int flag=0;
char[] c=Textbox1.Text.ToCharArray();
for(int i=0;i<c.length;i++)
{
if(c[i]!=" ")
{
flag=1;
break;
}
}
if(flag==0 || Textbox1.Text==""|| Textbox1.Text==null)
lblError.Text ="Enter required field";
this will check emptiness .null and whitespaces of TextBox