#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    19
    Rep Power
    0

    Question check condition with javascript


    i am writing out a form and so far i am pretty much done with the validations i need for this form.

    however, there is a part on my form where if the user selects option no.2 on a radio button, the user must type in(fill in) two fields...otherwise, if the user selects option no.1, he/she doesn't have to fill in the two fields...

    my question is: Is there a way where i can have it check if option 2 is selected and that a popup(alert) window will show up, telling the user that they must fill in the two fields?????

    here is some code that i've done for my form...

    ...
    <tr>
    <td>*Marital Status:</td>
    <td>
    <INPUT TYPE="radio" NAME="martlstat" value="sgl">Single&nbsp;
    <INPUT TYPE="radio" NAME="martlstat" value="mrd">Married</td>
    </tr>
    </table>
    <p><b>*For joint memberships, include spouse information.</b></p>
    <table border=0 cellpadding=0 cellspacing=0>
    <tr>
    <td>Spouse Name:</td>
    <td>&nbsp;<input type="text" size="23" name="spousename"></td>
    </tr>
    </table>
    .....
    <tr>
    <td>Did your spouse attend college?</td>
    <td>&nbsp;<input type ="radio" name="spouseattnd" value="Y">Yes</td>
    <td>&nbsp;<input type="radio" name="spouseattnd" value="N">No</td>
    </tr>
    </table>

    here, if the user selects the 2nd radio (mrd for married)... i want to have an alert window popup to tell the user that he/she needs to type in Spouse Name and if they attended college, which is radio button????

    anyones help out there is greatly appreciated...
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Rather than an alert - easy enough to do - this sort of thing often involves disabling/enabling the particular elements involved. See if this is of any use....

    <form>
    <table>
    <tr>
    <td>*Marital Status:</td>
    <td>
    <INPUT TYPE="radio" NAME="martlstat" value="sgl"
    onclick="enableDisable(this.form,this.checked)">Single
    <INPUT TYPE="radio" NAME="martlstat" value="mrd"
    onclick="enableDisable(this.form,!this.checked)">Married</td>
    </tr>
    </table>
    <p><b>*For joint memberships, include spouse information.</b></p>
    <table border=0 cellpadding=0 cellspacing=0>
    <tr>
    <td>Spouse Name:</td>
    <td> <input type="text" size="23" name="spousename" disabled="disabled"
    onfocus="if(this.disabled==null)this.blur()"></td>
    </tr>
    </table>
    <tr>
    <td>Did your spouse attend college?</td>
    <td> <input type ="radio" name="spouseattnd" value="Y" disabled="disabled"
    onclick="if(this.disabled==null)this.checked=false">Yes</td>
    <td> <input type="radio" name="spouseattnd" value="N" disabled="disabled"
    onclick="if(this.disabled==null)this.checked=false">No</td>
    </tr>

    </table>
    </form>
    <script language="JavaScript">
    function enableDisable(f, which) {
    if (f.spousename.disabled != null) f.spousename.disabled = which;
    else f.spousename.onfocus = (which) ? function() {this.blur()} : null;
    if (!which) {
    f.spousename.value = 'Enter Spouse Name';
    f.spousename.focus()
    f.spousename.select();
    } else f.spousename.value = '';
    if (which) f.spouseattnd[0].checked = false;
    if (f.spouseattnd[0].disabled != null) f.spouseattnd[0].disabled = which;
    else f.spouseattnd[0].onclick = (which) ? function() {this.checked=false} : null;
    if (which) f.spouseattnd[1].checked = false;
    if (f.spouseattnd[1].disabled != null) f.spouseattnd[1].disabled = which;
    else f.spouseattnd[1].onclick = (which) ? function() {this.checked=false} : null;
    }
    </script>
    Last edited by adios; May 22nd, 2002 at 07:38 PM.
  4. #3
  5. No Profile Picture
    aHVoPw==
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2001
    Posts
    1,058
    Rep Power
    15
    I'd stick with using the alert box, as some browsers doesn't have the ability to disable fields.
    Visit my new IIS Tutorial Series!
  6. #4
  7. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Alright; changed it, now it 'disables' in browsers that don't gray out fields.

IMN logo majestic logo threadwatch logo seochat tools logo