Home > front end >  if statement to check if the user has left the text box empty - issue
if statement to check if the user has left the text box empty - issue

Time:10-06

I have a web form with a text box where the user enters a URL so by default I have put an https:// but if the user does not enter a URL after and leaves it empty I want to show an error. I have used

if (textBox1.TextLength <= 9)
{
    // code
}

but that wont work if the user tries to put in a ftp URL.

How do i change my if statement to check if the user has left the text box empty besides the default https://

CodePudding user response:

You can use the StartsWith method.

if(textBox1.Text.StartsWith("https://") == true)
{
   ///your implementation 
}

CodePudding user response:

Instead of Length check, use equality operator with .Text property to check exact text in the text box.

//This means, there is only "https://" is written in the text box
if(textBox1.Text == "https://")
{
   //Show an error. Here user did not enter anything
}
else
{
    //Your implementation
} 
  • Related