简体   繁体   中英

Is there a way to determine required fields by checking a radio button?

We have consumer and business services, which customers can sign up for via a web form with the follow fields.

Full Name: Business Name: Email Address: Phone Number:

Is there a way to determine required fields by checking a radio button?

I would like to have Consumer and Business radio buttons. If you select the Consumer radio button, the Business Name field is not required but the rest of the fields are required. However, if you select the Business radio button than all fields are required including the Business Name.

Thanks!

You have 2 issues to deal with:

  1. On the HTML page, showing and hiding fields according to the radio button selection.
  2. Telling your server-side script to enforce the different required fields depending on the radio button selection.

For #1, you need to use JavaScript to manipulate the HTML page dynamically.

jQuery is a relatively easy way to do this.

For example:

<p>
   Business: <input type="radio" name="BusCons" id="Business" value="B" />
</p>
<p>
   Consumer: <input type="radio" name="BusCons" id="Consumer" value="C" />
</p>
<p>
   Business name: <input type="text" name="BusinessName" />
</p>
....
<script>
   $("#Business").click(function () {
       $("#BusinessName").show();
   });
   $("#Consumer").click(function () {
       $("#BusinessName").hide();
   });
</script>

For #2, you need a server-side script that can perform conditional field enforcement.

Tectite FormMail is one such script, using the "conditions" feature.

For example:

<input type="hidden" name="conditions" value=":@
  @IF@ BusCons ~ /B/ @
  BusinessName@
  @Please enter your business name@" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM