/    Sign up×
Community /Pin to ProfileBookmark

required fields set by radio button select

I am trying (if possible) to set required fields based on a specific value from radio button group. I have tried with my code below and it doesn’t work. The required fields work, but when I add the radioObject checked part, it does not work.

Any ideas?
Thanks!

<html>
<head>
<script type=”text/javascript”>
function validate_required(field,alerttxt)
{
if (radioObject[i].checked) {
value = radioObject[i].value.”list one”
break }

} return true;

{
with (field)
{
if (value==null||value==””)
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(email,”Email must be filled out!”)==false)
{email.focus();return false;}
}
{
if (validate_required(address,”Address must be filled out!”)==false)
{address.focus();return false;}
}
}
</script>
</head>

<body>
<form action=”submit.htm” onSubmit=”return validate_form(this)” method=”post”>
<p>List:
<input type=”radio” name=”radio” id=”list1″ value=”list one”>
<br>
List:
<input type=”radio” name=”radio” id=”list2″ value=”list two”>
<br>
List:
<input type=”radio” name=”radio” id=”list3″ value=”list three”>
</p>
<p>Email:
<input type=”text” name=”email” size=”30″>
</p>
<p>address:

<input type=”text” name=”address” size=”30″ id=”address”>
</p>
<p>
<input type=”submit” value=”Submit”>
</p>
</form>
</body>

</html>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@maezingauthorSep 23.2009 — Not sure if this is better formatted JavaScript, but trying a different approach and still not working.

<html>

<head>

<script type="text/javascript">

function checker(){

var buttonA=document.getElementById("list").checked;

var buttonB=document.getElementById("list").checked;

if(!buttonA.checked && !buttonB.checked){

alert=("Please choose from list");

}

if(buttonA.checked){

if(document.form.email.value==""){

alert=("Please fill in email");

}

}

if(buttonB.checked){

if(document.form.address.value==""){

alert=("Please fill in address");

}

}

return false;

}

else{

return true;

}

}

</script>

</head>

<body>

<form action="submit.htm" onSubmit="return checker()" method="post">

<p>Button A:

<input type="radio" name="radio" id="list" value="buttonA">

<br>

Button B:

<input type="radio" name="radio" id="list" value="buttonB">

<br>

</p>

<p>Email:

<input type="text" name="email" size="30" id="email">

</p>

<p>address:


<input type="text" name="address" size="30" id="address">

</p>

<p>

<input type="submit" value="Submit">

</p>

</form>

</body>

</html>
×

Success!

Help @maezing spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...