Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot change the RequiredFieldValidator to update another control like a text box, it will only update itself or a ValidationSummary if there is one on the page.</p> <p>2 options you have to get your desired output are:</p> <ol> <li>Use a <a href="http://www.4guysfromrolla.com/articles/073102-1.aspx" rel="nofollow">CustomValidator control</a></li> <li><a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=197" rel="nofollow">Create your own validation server control</a> that is capable of updating the css class of a textbox</li> </ol> <p>While most likely neither of these options will give you the "pure" HTML code you are looking for without any extra span or div tags, they will meet your other requirements provided you are willing to put in the time to write the JavaScript (or jQuery). However, considering the alternatives that are available (mostly client-side only validation via some framework), you are still better off sticking with ASP.NET validation because it can enforce the validation on the server side.</p> <p>The second option is more challenging, but a server control can be used on any page in your project or wrapped in a DLL to be deployed in multiple projects. With a CustomValidator there is some copy and paste if you want to duplicate the functionality on multiple controls, which can get difficult to maintain.</p> <p>For a real-world example of how you can use jQuery and custom server controls to get the results you are looking for, go to the checkout page on <a href="http://www.foldingchairdepot.com/" rel="nofollow">Folding Chair Depot</a> (you will need to add an item to the cart first to get there). When you submit the form without filling it out, the invalid textboxes are assigned new css classes which change their background color. However, because they are server controls, the functionality will work whether JavaScript is enabled or not.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload