/    Sign up×
Community /Pin to ProfileBookmark

Validating data

Trying to make sure a radio button is always checked on a form submit. I cant seem to get this to work. Please advise how I can correct this?

[code]
<script>
j=document.webpagename;
function validater(j)
{
for(x=0;x<j.color.length;x++)
{
if(j.color[x].checked)
var checked=true;

if(!checked)
{
alert(‘One color has to be selected.’);
return false;
};

}

<form method=”post” action=”actionpage” onsubmit=”return validater(this)”
<blockquote>
<input name=”color” type=”radio” value=”red”>
<input name=”color” type=”radio” value=”green”>
<input name=”color” type=”radio” value=”blue”>
<input name=”color” type=”radio” value=”black”>
</blockquote>

[/code]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@floridaauthorSep 23.2003 — I do have the </script> part but didnt include it in my original post. Please advise how I can get this to work. Thanks.

<i>
</i>&lt;script&gt;
j=document.webpagename;
function validater(j)
{
for(x=0;x&lt;j.color.length;x++)
{
if(j.color[x].checked)
var checked=true;

if(!checked)
{
alert('One color has to be selected.');
return false;
};

}
&lt;/script&gt;

.....
&lt;form method="post" action="actionpage" onsubmit="return validater(this)"
&lt;blockquote&gt;
&lt;input name="color" type="radio" value="red"&gt;
&lt;input name="color" type="radio" value="green"&gt;
&lt;input name="color" type="radio" value="blue"&gt;
&lt;input name="color" type="radio" value="black"&gt;
&lt;/blockquote&gt;


Copy linkTweet thisAlerts:
@FangSep 23.2003 — <i>
</i>function validater(j) {
for(var x=0;x&lt;j.color.length;x++) {
if(j.color[x].checked) {
return;
}
}
alert('One color has to be selected.');
return false;
}
Copy linkTweet thisAlerts:
@CharlesSep 23.2003 — [font=monospace]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<style type="text/css">

<!--

form div {margin:0em 3em}

label {display:block}

button {margin-top:1em}

-->

</style>

<script type="text/javascript">

<!--

function validate (f) {

var i;

var checked;

for (i=0; i<f.color.length; i++) {if (f.color[i].checked) checked = true};

if (!checked) {alert ('Pick a color.'); f.focus(); return false}

}

// -->

</script>



<form action="" onsubmit="return validate(this)">

<div>

<label><input name="color" type="radio" value="red">Red</label>

<label><input name="color" type="radio" value="green">Green</label>

<label><input name="color" type="radio" value="blue">Blue</label>

<label><input name="color" type="radio" value="black">Black</label>

<button type="submit">Submit</button>

</div>

</form>[/font]
Copy linkTweet thisAlerts:
@CharlesSep 23.2003 — [font=georgia]Ignore the following, the first cup of Earl Grey hasn't fully kicked in.


Fang, run this...[/font]

[font=monospace]<script type="text/javascript">

<!--

i=1;

function foo () {for (i=0; i<9; i++){}};

foo();

alert(i);

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@FangSep 23.2003 — and the point being ... you change a global variable, I use local variables in loops
Copy linkTweet thisAlerts:
@CharlesSep 23.2003 — [i]Originally posted by Fang [/i]

[B]I use local variables in loops [/B][/QUOTE]
[font=georgia]Something I failed to notice until after I had posted my second above. But by then you had begun to read it and there was no way to delete it without causing more harm.[/font]
Copy linkTweet thisAlerts:
@FangSep 23.2003 — easily missed ?
Copy linkTweet thisAlerts:
@floridaauthorSep 24.2003 — Thanks for all the help!
×

Success!

Help @florida 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...