Required field:

<asp:textbox id="textbox1" runat="server"/>
<asp:RequiredFieldValidator id="valRequired" runat="server"
ControlToValidate="textbox1" ErrorMessage="* Please enter a value" Display="dynamic">*
</asp:

how can i put the vlidation correctly

Dani AI

Generated

— the snippet you posted is just malformed HTML/ASP.NET tags (missing a proper closing tag and mixed case). Below is a minimal, correct example and a few quick checks to get validation working reliably.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="valRequired" runat="server"
  ControlToValidate="TextBox1"
  ErrorMessage="Please enter a value"
  Display="Dynamic" />

On the server side, always guard processing with Page.IsValid so you do not act on invalid input:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (!Page.IsValid) return;
    // safe to process TextBox1.Text here
}

Troubleshooting checklist

  • Ensure the control you reference has runat="server" and the ControlToValidate string exactly matches the control ID.
  • Use Display="Dynamic" if you want the error message not to reserve layout space; Display="Static" reserves space and avoids layout shifts.
  • If client-side errors never appear, check the browser console for JavaScript errors (client validation relies on the rendered scripts).
  • If you need pattern/format checking rather than just required/not-empty, consider the appropriate validator type as mentioned by .

Additional notes: — if there are special requirements (length, numeric ranges, custom rules), include them so the best validator choice can be suggested. and — the above addresses the immediate markup and server-side checks that usually stop validation from working. For full reference see the Microsoft docs on validators and Page.IsValid (RequiredFieldValidator, Page.IsValid).

Recommended Answers

All 4 Replies

Hi, what seems to be the problem?

What the code you posted does is ...
once a buttonis clicked,
it will check if textbox1 is empty, if it is empty it will show the ErrorMessage.

If you want to validate for correct input, use RegularExpressionValidator

Hope this helps.

keyur.smith just explain your exact requirement for validation. Then we will help you better.

Dear the asp.net validation occurs on the client side for asp.net controls
The validation occurs usually on button click , you can set the button and controls intended to be validated in the same validation group
you can prevent the button click validation by set the property causes validation to false , for example the auto post back for check box or auto post back of drop down list can cause validation by set the causes validation to true of check box and drop down list asp.net controls
Now the above validation you mentioned you can extend by controls on AJAX Control Toolkit , to have a better look and feel

if problem resolved then mark this thread as resolved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.