views:

253

answers:

3

Hi all, I'm pretty new to this so here goes...

I'm using Visual Studio 05 (C#) and in my program I have a textbox and a submit button. The user enters an email address and results are then displayed from the database (this works) using an ASP gridview control.

What I am after is a simple piece of validation that if nothing has been entered into the textbox, display a message (or a popup) to say that something needs to be entered.

Many thanks!

A: 

Use the RequiredFieldValidator.

<asp:RequiredFieldValidator ID="Id1" runat="server" ErrorMessage="*" ValidationGroup="1" ControlToValidate="txt_Test" />                                        
<asp:TextBox runat="server" ID="txt_Test" />

You can use the CustomValidator for displaying a popup, just provide it your own javascript function.

GenericTypeTea
Cheers for the fast reply, this works great.
Mike
Any time. That's what SO is for!
GenericTypeTea
A: 

On the client side this bit of jQuery code could help

$(function(){
  $('#id_of_form').submit(function(e){
    if($.trim($('#id_of_textbox').val()) === '') {
      alert('Textbox cannot be empty');
      return false;
    }
    return true;
  });
});
Alex Sexton
A: 

If you're using WinForms, you can do the following:

    if (String.IsNullOrEmpty(txt_Test.Text.Trim()))
    {
        MessageBox.Show("You must enter something.");
    }
GenericTypeTea