ASP.NET form wizard optional fields?

1.8k views Asked by At

I made an ASP.NET form wizard that contains multiple pages of fields. Some of these fields are meant to be required and some are meant to be optional. By default the wizard seems to make every field required. How do I change them to optional? Form Wizard

I assumed that required=false in <input runat="server" ID="txtDriverLicenseNumber" type="text" required="false" size="24"></input> would have the desired effect, but it seems to do nothing.

Additionally, I would like the form to allow me to hit "previous" even if I did not fill out the fields on the current page.

Some assistance would be greatly appreciated!

2

There are 2 answers

0
EdSF On BEST ANSWER
  1. HTML5 attribute required for input fields is validated on the client side (browser). Not all browsers do. Don't include the attribute if you don't want it to be reqiured (leave it out, don't say required="false")

    This input is required:

    <input required name=... /> 
    

    This input is not required:

    <input name=... /> 
    
  2. You can use the attribute in HTML server controls or ASP.Net controls to add client side validation for browsers that do support it:

    Use on HTML server control:

    <input id="input1" name="input1" type="text" required runat="server" />
    

    Use on ASP.net control:

    <asp:TextBox runat="server" ID="tbox1" required />
    

    This is how Firefox (v12) renders validation of required attribute (your screen shot is Chrome?):

    firefox rendering of required input attribute

  3. You can use ASP.Net Validation Controls to validate both HTML server and ASP.net controls on the server (aka server-side validation) and is what you should do in order to properly validate your inputs.

    Server side validation will work regardless of whether or not the browser supports HTML5 required attribute:

    Server side validation (required) of ASP.Net HTML server control:

    <input id="input1" name="input1" type="text" required runat="server" />
    
    <asp:RequiredFieldValidator ID="rf1" runat="server" 
    Text="input required" ControlToValidate="input1" ForeColor="Red" />
    

    Server side validation (required) of ASP.Net control:

    <asp:TextBox runat="server" ID="tbox1" required />
    
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
    Text="textbox required" ControlToValidate="tbox1" ForeColor="Red" />
    
  4. Validation will control your progress through the ASP.Net Wizard - required fields (only) must be provided for you to go to the "next step" (and back). That is how it should work.

    I think you're just experiencing difficulty because you have made all your fields required in error (because you are setting the required="false" attribute instead of omitting it and you are using Chrome - which does validate it).

Hth...

4
Antonio Bakula On

You are using HTML input tag attribute required, it's not ASP.NET server control property. If required attribute is present then field is required regardless of it's value. Be aware that Required property is supported only in HTML 5.

http://www.w3schools.com/html5/att_input_required.asp

ASP.NET wizard control doesn't make fields required, ASP.NET is using validation controls and it would be better to use that. And with validators you have to use ASP.NET server controls (TextBox, CheckBox etc.), see here :

http://msdn.microsoft.com/en-us/library/debza5t0.aspx

If you use ASP.NET validators then you can just set CausesValidation button property to false and that button would not trigger validation.