Search code examples
asp.netxmlserver-side

How do I do a "Is Required" test on text field?


I'm trying to make a registration form for a ASP.NET project, it has to include a "Is Required" test for each field. Anyone have any good resources on this?


Solution

  • If this is WebForms you can use RequiredFieldValidator. Here is a simple example that would valid a text box named "txtName":

    <asp:RequiredFieldValidator runat="server" 
            ControlToValidate="txtName"
            ErrorMessage="User ID is required."> *
    </asp:RequiredFieldValidator>
    

    Here is an MSDN article that talks about exactly what you want and gives examples of other validators (again, assuming this is WebForms):

    https://msdn.microsoft.com/en-us/library/ms972961.aspx

    And a code project article with more examples and info:

    http://www.codeproject.com/Articles/334310/Understanding-ASP-NET-Validation-Techniques