Travis
Travis

Reputation: 135

How to require text in a Textbox?

How do I go about requiring text in a textbox? This is what i have so far.

String strName = txtName.Text;
String strEmail = txtEmail.Text;
Boolean blnErrors = false;

if (strName == null)
{

}
else
{
    string script = "alert(\"Name Field Is Required!\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);

    txtName.Focus();
}

When I run the program and try to execute it, I get the error popping up regardless if I have text entered into the textbox or not. I only want the Error to show if There is Nothing in the TextBox. I have also tried using,

if (strName == "")

as well. But nothing changes.

Upvotes: 2

Views: 3612

Answers (5)

Jovino Rodríguez
Jovino Rodríguez

Reputation: 84

It seems to me using ScriptManager to do this kind of client validation is a bit overwhelming. A simple RequireFieldValidator will do what you are trying to do.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator(v=vs.110).aspx

Upvotes: 3

matt_lethargic
matt_lethargic

Reputation: 2796

As user3402321 said using a RequireFieldValidator is the correct approach for this.

HTML:

<asp:TextBox runat="server" ID="txtEmail" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" >Please enter an email address</asp:RequiredFieldValidator>

C#:

if(Page.IsValid)
{
    // Process submisison
}

If all the validators on the page pass validation Page.IsValid will be true, if one validator fails, IsValid will be false. Also for an email address you might want to use a RegEx validator to check the email format is correct:

<asp:RegularExpressionValidator runat="server" ControlToValidate="txtEmail" ValidationExpression="<your favourite email regex pattern>" Text="Email not correct format" />

Obviously change <your favourite email regex pattern> to an email regex pattern of your chosing.

EDIT Further to what I've already said you can user a <asp:ValidationSummary /> control to show all of the validation errors in one place and if you set the ShowMessageBox property to true it will display the message in a javascript alert() message box.

Upvotes: 0

user3363925
user3363925

Reputation: 23

if (txtName.TextLength==0)
{
//code
}

Upvotes: 0

Travis
Travis

Reputation: 135

I got the answer on my own. Here is the correct answer of what I wanted.

if (txtName.Text == "")
        {
            string script = "alert(\"Name Field Is Required!\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true);

            txtName.Focus();
        }

This makes it so If the textbox is blank, it will show an error message. Otherwise, if there is text in the TextBox, nothing will happen. This is what I wanted.

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148664

Change your code to this :

String strName = txtName.Text.Trim(); //add trim here
String strEmail = txtEmail.Text;
Boolean blnErrors = false;

if (string.IsNullOrWhiteSpace(sstrName)) //this function checks for both null or empty string.
{
    string script = "alert(\"Name Field Is Required!\");";
    ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
    txtName.Focus();
    return;//return from the function as there is an error.
}

//continue as usual .

Upvotes: 1

Related Questions