RequiredFieldValidater to textbox

I am creating a social network in asp.net C #,

So my textbox needs a RequiredFieldValidater Like this

can someboday tell me how to add this. Please fill in this validater for text fields on my website, Now I am using this code,

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="Your_Name" ErrorMessage="Enter Your Name" 
                        ForeColor="Red" ValidationGroup="RegisterGroup"></asp:RequiredFieldValidator>

      

+3


source to share


1 answer


You must use the required attribute with your server control not with a validation control

  <form id="form1" runat="server">
        Your name:<br />
        <asp:TextBox runat="server" id="txtName" required="required"/>
        <asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator1" controltovalidate="Your_Name" ForeColor="Red" errormessage="Please enter your name!" />
        <br /><br />
        <asp:Button runat="server" id="btnSubmitForm" text="Ok" onclick="btnSubmitForm_Click" />
    </form>

      



In the event handler of the CodeBehind file:

protected void btnSubmitForm_Click(object sender, EventArgs e)
{
    if(Page.IsValid)
    {
        btnSubmitForm.Text = "My form is valid!";
    }
}

      

0


source







All Articles