Click to See Complete Forum and Search --> : Validating data


florida
09-23-2003, 06:00 AM
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?


<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>

florida
09-23-2003, 06:04 AM
I do have the </script> part but didnt include it in my original post. Please advise how I can get this to work. Thanks.


<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;
};

}
</script>

.....
<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>

Fang
09-23-2003, 06:37 AM
function validater(j) {
for(var x=0;x<j.color.length;x++) {
if(j.color[x].checked) {
return;
}
}
alert('One color has to be selected.');
return false;
}

Charles
09-23-2003, 06:38 AM
<!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>

Charles
09-23-2003, 06:42 AM
Ignore the following, the first cup of Earl Grey hasn't fully kicked in.


Fang, run this...

<script type="text/javascript">
<!--
i=1;
function foo () {for (i=0; i<9; i++){}};
foo();
alert(i);
// -->
</script>

Fang
09-23-2003, 06:56 AM
and the point being ... you change a global variable, I use local variables in loops

Charles
09-23-2003, 07:04 AM
Originally posted by Fang
I use local variables in loops 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.

Fang
09-23-2003, 08:09 AM
easily missed ;)

florida
09-24-2003, 12:28 PM
Thanks for all the help!