The first section contains 5 form fields and are required.
The second second has 2 form fields and are optional.
Users can only choose from one section or the other but not both.
The js script below ensures that if users select from section 2, section one will not challenge for validation and works a treat.
The challenge I have now is to ensure that if users choose from section 1, the 2 form fields in section are automatically disabled so they won't be attempted to choose from it.
Similarly, if users select from section 2, all form fields in section one are automatically disabled so they won't be tempted to choose from that section.
Is this possible?
If not possible, is it possible to raise an error?
For instance, a user selects from section 1 and attempts to select from any of the 2 form fields in section 2, a message that says, "You can only select from one section but from both.
Similarly if a user selects from section 2 first, same thing happens.
Thanks very much for your assistance.
function validate() {
if(document.getElementById("<%=txtrequest.ClientID%>").value != "" &&
document.getElementById("<%=txtreceived.ClientID %>").value != "")
{
if (document.getElementById("<%=txtName.ClientID%>").value == "") {
alert("Please enter name");
document.getElementById("<%=txtName.ClientID%>").focus();
return false;
}
if (document.getElementById("<%=txtCity.ClientID %>").value == "") {
alert("Please enter city");
document.getElementById("<%=txtCity.ClientID %>").focus();
return false;
}
if (document.getElementById("<%=txtState.ClientID%>").value == "") {
alert("Please enter state");
document.getElementById("<%=txtState.ClientID%>").focus();
return false;
}
if (document.getElementById("<%=txtZip.ClientID%>").value == "") {
alert("Please enter zip");
document.getElementById("<%=txtZip.ClientID%>").focus();
return false;
}
if (checkradio() == false) {
alert("Please enter contacts");
document.getElementById("<%=txtContacts.ClientID%>").focus();
return false;
}
}
return true;
}
According to me there are two solution first You can used custom validator and in this validator check the fields in section 2 and section 1 both have selected then raise error.
Second in every field selection called custome validator and check is there any other field selected in different section or not then raise error.
Farrukh Abbas
~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~
You can do this. It depends of what type of controls you have in both side. Suppose you have only textboxes in both the side. What you can do is on blur of any control you can check if the control in the containing section have some value. If so then disable
the containing div of the other section. Otherwise enable the other section. Suppose the required field section is disabled, then you can change the required field validation based on the disabled or enabled status of the section.
Thanks & Regards
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
The challenge I have now is to ensure that if users choose from section 1, the 2 form fields in section are automatically disabled so they won't be attempted to choose from it.
And how to you suppose to let user to choose the section two if they want to fill out the section two? Once you disable the controls in section two, you will no longer be able to select them.
simflex
Similarly, if users select from section 2, all form fields in section one are automatically disabled so they won't be tempted to choose from that section.
Again you will face with the similar problem.
Why don't you use radio button list with two radio buttons? One will enable the section 1 and other one will enable the section 2. Only one will be enable at a time. That's what you want right? Simply use a radiobutton list. It's the convenient way, anyway.
To disable the element from javascripts, you can use the below code. Use it for your controls and disable them based on the selection of radio buttons list.
simflex
Member
79 Points
274 Posts
How can I disable some form fields when selecting others
Apr 30, 2012 04:19 AM|LINK
Greetings experts
I need your help one more time.
I have makup with 2 sections.
The first section contains 5 form fields and are required.
The second second has 2 form fields and are optional.
Users can only choose from one section or the other but not both.
The js script below ensures that if users select from section 2, section one will not challenge for validation and works a treat.
The challenge I have now is to ensure that if users choose from section 1, the 2 form fields in section are automatically disabled so they won't be attempted to choose from it.
Similarly, if users select from section 2, all form fields in section one are automatically disabled so they won't be tempted to choose from that section.
Is this possible?
If not possible, is it possible to raise an error?
For instance, a user selects from section 1 and attempts to select from any of the 2 form fields in section 2, a message that says, "You can only select from one section but from both.
Similarly if a user selects from section 2 first, same thing happens.
Thanks very much for your assistance.
function validate() { if(document.getElementById("<%=txtrequest.ClientID%>").value != "" && document.getElementById("<%=txtreceived.ClientID %>").value != "") { if (document.getElementById("<%=txtName.ClientID%>").value == "") { alert("Please enter name"); document.getElementById("<%=txtName.ClientID%>").focus(); return false; } if (document.getElementById("<%=txtCity.ClientID %>").value == "") { alert("Please enter city"); document.getElementById("<%=txtCity.ClientID %>").focus(); return false; } if (document.getElementById("<%=txtState.ClientID%>").value == "") { alert("Please enter state"); document.getElementById("<%=txtState.ClientID%>").focus(); return false; } if (document.getElementById("<%=txtZip.ClientID%>").value == "") { alert("Please enter zip"); document.getElementById("<%=txtZip.ClientID%>").focus(); return false; } if (checkradio() == false) { alert("Please enter contacts"); document.getElementById("<%=txtContacts.ClientID%>").focus(); return false; } } return true; }morefays
Participant
1207 Points
255 Posts
Re: How can I disable some form fields when selecting others
Apr 30, 2012 05:37 AM|LINK
According to me there are two solution first You can used custom validator and in this validator check the fields in section 2 and section 1 both have selected then raise error.
Second in every field selection called custome validator and check is there any other field selected in different section or not then raise error.
~Please Mark As Answer, if one or multiple posts, which helped you in your problem. So that it might be useful for others~
dotnetfarrukhabbasblogspot
asteranup
All-Star
30184 Points
4906 Posts
Re: How can I disable some form fields when selecting others
Apr 30, 2012 10:27 AM|LINK
Hi,
You can do this. It depends of what type of controls you have in both side. Suppose you have only textboxes in both the side. What you can do is on blur of any control you can check if the control in the containing section have some value. If so then disable the containing div of the other section. Otherwise enable the other section. Suppose the required field section is disabled, then you can change the required field validation based on the disabled or enabled status of the section.
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
Ruchira
All-Star
44181 Points
7179 Posts
MVP
Re: How can I disable some form fields when selecting others
Apr 30, 2012 12:28 PM|LINK
Hello,
And how to you suppose to let user to choose the section two if they want to fill out the section two? Once you disable the controls in section two, you will no longer be able to select them.
Again you will face with the similar problem.
Why don't you use radio button list with two radio buttons? One will enable the section 1 and other one will enable the section 2. Only one will be enable at a time. That's what you want right? Simply use a radiobutton list. It's the convenient way, anyway. To disable the element from javascripts, you can use the below code. Use it for your controls and disable them based on the selection of radio buttons list.
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.