Click to See Complete Forum and Search --> : <JavaScript> question.


acf01
May 27th, 2005, 12:35 PM
I have a quick question, and would greatly appreciate a jumpstart on this one!

I have a question with 2 radio buttons, 1 for yes, and 1 for no.

what I am trying to do is to see that if the no button is selected, then the text area box that pops up would be a required field, requiring entry as to why no was selected. I am close, but I've tried "if" statements and just about everything else my intermediate ability in js can conjure up. Adding Required to the text area tag still makes it required if "yes" is selected.

I am stuck and need some help...

Here is the code so far: ( I took the incorrect "if" statements out, because they weren't working anyway)

<html>
<head>
<title>Untitled</title>

<SCRIPT LANGUAGE='JavaScript'>

function isEmpty(str) {
// Check whether string is empty.
for (var intLoop = 0; intLoop < str.length; intLoop++)
if (" " != str.charAt(intLoop))
return false;
return true;
}

function checkRequired(f) {
var strError = "";
for (var intLoop = 0; intLoop < f.elements.length; intLoop++)
if (null!=f.elements[intLoop].getAttribute("required"))
if (isEmpty(f.elements[intLoop].value))
strError += " " + f.elements[intLoop].name + "\n";
if ("" != strError) {
alert("Required data is missing:\n" + strError);
return false;
}


}
</SCRIPT>



<SCRIPT>
function no() {
MMDiv.style.visibility='visible';
Alansform.ReasonWhy.focus();


}

function yes() {
MMDiv.style.visibility='hidden';

}


</SCRIPT>


</head>

<body>
<FORM NAME="Alansform" onsubmit="return checkRequired(this);">
Is The Update Complete?
<INPUT TYPE="RADIO" NAME="Updated" onclick="yes();"CHECKED>Yes
<INPUT TYPE="RADIO" NAME="Updated" onclick="no();" >No
<BR>
<DIV ID="MMDiv" style="visibility:hidden">
<LABEL FOR="ReasonWhy">
<TextArea name="Comments" id="ReasonWhy" onkeypress="LimitMultiLineLength(this)" onbeforepaste="LimitMultiLineLength(this)" maxLength="100" ErrorMessage="The maximum allowance of 100 characters has been reached." style="height:108px;width:400px;"></textarea>
Please enter reason why</LABEL>
</DIV>
<BR>
<INPUT TYPE="SUBMIT">
<INPUT TYPE="RESET">
</FORM>



</body>
</html>

Thanks for anyhelp!

PeejAvery
June 8th, 2005, 01:16 PM
Why not just add another variable to the application?

function no(){
isrequired = true;
MMDiv.style.visibility='visible';
Alansform.ReasonWhy.focus();
}

function yes(){
isrequired = false;
MMDiv.style.visibility='hidden';
}

Now make it so when it checks the form before submitting...
if(isrequired=="true"){
if(Alansform.comments.value==""){window.alert("Missing info");}
else{document.Alansform.submit();}
}

Now change the submit button to a regular button that calls a check form subroutine.