Make one field mandatory if another field has certain text in it

Posted on 2006-06-02
Last Modified: 2010-04-06
Hi all.

Not sure where this belongs So thought i'll put it here.

I need to make a field madatory only if a certain response has been made in another field. ie,

Field1 has three options (Mandatory)


Field2 (Will be email or Number).

If someone choses option one, it inserts the text into the database
If option2 is chosen, then a email address is needed
If option3 is chosen, then a number is required.

The system i use is Dreamweaver for my madnatory fields selection normally and code is ASP VBScript.

Any help would be appreciated.

Question by:Bakersville
    LVL 6

    Accepted Solution

    This should give you an idea of how this can be done:

    <form name="form" method="post" action="">

    <select name="field1" onchange="updateForm()">

    <div id="field2" style="display: none">
    <span id="field2Text"></span>&nbsp;<input type="text" name="field2">

    <script language="javascript">
    function updateForm(field1) {
        var field1 = document.form.field1.options[document.form.field1.selectedIndex].text;

        if (field1 == "Option1") {
            document.getElementById("field2").style.display = "none";
        else if (field1 == "Option2") {
            document.getElementById("field2").style.display = "inline";
            document.getElementById("field2Text").innerHTML = "email";
        else if (field1 == "Option3") {
            document.getElementById("field2").style.display = "inline";
            document.getElementById("field2Text").innerHTML = "number";
    LVL 1

    Author Comment

    Hi inviser

    thank you for yu response.  I am on Hols at the moment and will give it a try once i return in a week monday.  



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now