Link to home
Start Free TrialLog in
Avatar of SherryG
SherryG

asked on

Making form fields required

I have a form with textfields and radio buttons(groups).  For instance....


What is your favorite color  
a. red (radio)  
b. blue (radio)  
c. green (radio)
d. other (radio)-please specify _______________ (text)


I want to make the entire question required, so the user must select atleast one of the radio buttons and if they select (d) I want to also make the text field required.

I have many questions like this on my form.

Any suggestions?

Avatar of HamdyHassan
HamdyHassan

yes you can using javascript
not necesserily javascript:

say you radiobutton named "aaa"

then make just another input
<INPUT TYPE="hidden"
    NAME="aaa_required"
    VALUE="Please, select your favorite color.">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
     <title>Untitled</title>
<script language="JavaScript">
      function fncheck(){
         if ( document.testform.rdcolor[3].checked){
             if (document.testform.txtOthers.value == ""){
               alert("Specify the other")
                document.testform.txtOthers.focus()
                return false
             }
          }  
         
      }  
</script>    

</head>

<body>
<form name="testform">
   <br><input type="Radio" name="rdcolor" checked>Red
   <br><input type="Radio" name="rdcolor">Blue
   <br><input type="Radio" name="rdcolor">Green
   <br><input type="Radio" name="rdcolor">Other &nbsp;<input type="Text" name="txtOthers" value="">
   <br><input type="button" name="btncheck" value="check" onclick="fncheck()" >
</form>



</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of anandkp
anandkp
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial