/    Sign up×
Community /Pin to ProfileBookmark

javascript radio button verification

hello my delimma is simple i have a set od 2 radio buttons one says yes and one says no i want to check on click as part of the function i have already and make it check to see the user has selected one and if they havent then pop up and say please check option yes or no here is the function i have currently and need to add this too…

[CODE]function validateInputs2() {
if (document.frmsignup.fieldname.value == “”) {
alert(“Please enter your Name”);
document.frmsignup.fieldname.focus();
return false;
} else if (document.frmsignup.fieldaddress.value == “”) {
alert(“Please enter your Address”);
document.frmsignup.fieldaddress.focus();
return false;
} else if (document.frmsignup.fieldcity.value == “”) {
alert(“Please enter your City”);
document.frmsignup.fieldcity.focus();
return false;
} else if (document.frmsignup.fieldstate.value == “”) {
alert(“Please enter your State”);
document.frmsignup.fieldstate.focus();
return false;
} else if (document.frmsignup.fieldzip.value == “”) {
alert(“Please enter your Zip Code”);
document.frmsignup.fieldzip.focus();
return false;
} else if (isNaN(document.frmsignup.fieldzip.value) == true) {
alert(“Zip Code must be a 5 digit number”);
document.frmsignup.fieldzip .select();
return false;
} else if (document.frmsignup.fieldphone.value == “”) {
alert(“Please enter your Phone Number”);
document.frmsignup.fieldphone.focus();
return false;
}else if (document.frmsignup.fieldacresFarmed.value == “”) {
alert(“Please enter how many acres of soybeans farm”);
document.frmsignup.fieldacresFarmed.focus();
return false;
}else if (document.frmsignup.fieldradSpeak.value == “”) {
alert(“Please enter if you can do public speeking or speak to the media”);
document.frmsignup.fieldradSpeak.focus();
return false;
}else if (document.frmsignup.fieldFarming.value == “”) {
alert(“Please describe your farm”);
document.frmsignup.fieldFarming.focus();
return false;
}

}

[/CODE]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 14.2006 — [code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Example</title>

<script type="text/javascript">
function check (f) {
var checked = false, e, i = 0
while (e = f.elements[i++]) {
if (e.type == 'text' && !/S/.test (e.value)) {
alert ('Please, field "' + e.previousSibling.data + '" is required.')
e.focus()
return false
}
else if (e.type == 'radio' && e.checked) checked = true
}
if (!checked) {
alert ('Please, yes or no.');
return false
}
}
</script>

<style type="text/css">
form {margin:auto; width:10em}
label, button {display:block; margin:1em auto}
</style>

</head>
<body>
<form action="some-script.pl" onsubmit="return check (this)">
<fieldset>
<legend>Yes or no</legend>
<label><input name="yesOrNo" type="radio">Yes</label>
<label><input name="yesOrNo" type="radio">No</label>
</fieldset>

<fieldset>
<legend>Example</legend>
<label>Name <input></label>
<label>Rank <input></label>
<label>Serial number <input></label>
</fieldset>

<div>
<button type="submit">Submit</button>
</div>
</form>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@Codeman0013authorAug 14.2006 — charles how would that function integrate with teh above function i posted earlier?
Copy linkTweet thisAlerts:
@CharlesAug 14.2006 — charles how would that function integrate with teh above function i posted earlier?[/QUOTE]It would integrate quite well. But you might consider using the method I demonstrated above to validate text fields. It's a good bit less typing and more portable.
Copy linkTweet thisAlerts:
@Codeman0013authorAug 14.2006 — It would integrate quite well. But you might consider using the method I demonstrated above to validate text fields. It's a good bit less typing and more portable.[/QUOTE]


grr i swear i'm an idiot i took your example and tested it and it works but as soon as i try to paste it into my page it does not work do i have to change something? for example my names of my radio group would be fieldradMeet??
Copy linkTweet thisAlerts:
@CharlesAug 14.2006 — Well, you're going to have to make some modifications - to either your form or to the script. Study the thing. You'll note that it makes no use of "names" at all.
Copy linkTweet thisAlerts:
@Codeman0013authorAug 14.2006 — i cannot modify the form as that would cause me to modify like 3 other pages as well lol where in the script wold i have to modify i'm totally a new person to this sorry man..
Copy linkTweet thisAlerts:
@CharlesAug 14.2006 — Well then, you'll have to modify the script.
×

Success!

Help @Codeman0013 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.18,
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,
)...