...

View Full Version : Problems validating radio buttons



subnet_rx
08-01-2006, 08:19 PM
I'm trying to validate a set of radio buttons along with many other fields. With each field, I do a check to see if it's empty, if it is, then the script alerts and returns false. It's working for everything but the radio buttons (and drop-downs , but I guess that's another issue). The form name is form and the field is compability. Here is my code for validating radio buttons:



var radio_choice = false
// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < document.form.compability.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (document.form.compability[counter].checked)
radio_choice = true
}

if (!radio_choice)
{
// If there were no selections made display an alert box
alert("Please select your computer ability")
return false
}

vwphillips
08-01-2006, 08:51 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
function Valid(){
var frm=document.form;
var mess=['Invalid'];
var radio_choice = false
var rad=frm.compability;
mess.push('Check 1 from '+(rad[0].title||rad[0].name));
for (counter = 0; counter < rad.length; counter++){
if (rad[counter].checked){ mess.length--; }
}
var rad=frm.compability1;
mess.push('Check 1 from '+(rad[0].title||rad[0].name));
for (counter = 0; counter < rad.length; counter++){
if (rad[counter].checked){ mess.length--; }
}
if (mess.length>1){
alert(mess.join('\n'));
return false;
}
return true

}
/*]]>*/
</script>
</head>

<body>
<form name="form" >
<input type="radio" name="compability" >
<input type="radio" name="compability" >
<input type="radio" name="compability" >
<input title="Second Group" type="radio" name="compability1" >
<input type="radio" name="compability1" >
<input type="radio" name="compability1" >
<input type="button" value="Test" onclick="Valid();" >
</form>
</body>

</html>

subnet_rx
08-02-2006, 03:32 PM
I still couldn't get it to work. I'll just post my full script (with VWPhillips additions) to try to get an idea of what I'm doing wrong. As I said, all other fields are working, except for the radio buttons.



function ValidateForm(){
var emailID=document.form.email

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your email")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
if ((document.form.major.value==null)||(document.form.major.value=="")){
alert("Please Enter your major")
document.form.major.focus()
return false
}
if ((document.form.techcourses.value==null)||(document.form.techcourses.value=="")){
alert("Please Enter how many technology courses you have taken")
document.form.techcourses.focus()
return false
}
if ((document.form.age.value==null)||(document.form.age.value=="")){
alert("Please Enter your age")
document.form.age.focus()
return false
}
if ((document.form.rank.value==null)||(document.form.rank.value=="")){
alert("Please enter your college rank")
document.form.rank.focus()
return false
}
if ((document.form.ipsub.value==null)||(document.form.ipsub.value=="")){
alert("Do you have an ISP?")
document.form.ipsub.focus()
return false
}
if ((document.form.lengthuse.value==null)||(document.form.lengthuse.value=="")){
alert("Please Enter your computer use")
document.form.lengthuse.focus()
return false
}
if ((document.form.help.value==null)||(document.form.help.value=="")){
alert("Please fill out the required fields")
document.form.help.focus()
return false
}
if ((document.form.otherhelp.value==null)||(document.form.otherhelp.value=="")){
alert("Please fill out the required fields")
document.form.otherhelp.focus()
return false
}
var frm=document.form;
var mess=['Invalid'];
var radio_choice = false
var rad=frm.compability;
mess.push('Check 1 from '+(rad[0].title||rad[0].name));
for (counter = 0; counter < rad.length; counter++){
if (rad[counter].checked){ mess.length--; }
}
var rad=frm.compability1;
mess.push('Check 1 from '+(rad[0].title||rad[0].name));
for (counter = 0; counter < rad.length; counter++){
if (rad[counter].checked){ mess.length--; }
}
if (mess.length>1){
alert(mess.join('\n'));
return false;
}
return true
}
</script>

vwphillips
08-02-2006, 04:06 PM
tested with IE 6 & Moz FF


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script>

function ValidateForm() {
var frm=document.form;
if (window['f64Marks']){ f64Marks(frm); }
var mess=['Invalid'];
if (frm.email.value.length<1){
mess.push("Please Enter your email");
if (window['f64Marks']){ f64MakeMark(frm.email,100,5); }
}
if (frm.major.value.length<1){
mess.push("Please Enter your major")
if (window['f64Marks']){ f64MakeMark(frm.major,100,5); }
}
if (frm.techcourses.value.length<1){
mess.push("Please Enter how many technology courses you have taken")
if (window['f64Marks']){ f64MakeMark(frm.techcourses,100,5); }
}
if (frm.age.value.length<1){
mess.push("Please Enter your age")
if (window['f64Marks']){ f64MakeMark(frm.age,100,5); }
}
var rad=frm.compability;
mess.push('Check 1 from '+(rad[0].title||rad[0].name));
if (window['f64Marks']){ f64MakeMark(rad[0],20,5); }
for (counter = 0; counter < rad.length; counter++){
if (rad[counter].checked){
mess.length--;
if (window['f64Marks']){ f64RemoveMark(frm); }
}
}
if (mess.length>1){
alert(mess.join('\n'));
return false;
}
return true
}
</script>
<script language="JavaScript" type="text/javascript">
<!--
// Mark Failing Fields (26-May-2006)
// by Vic Phillips http://www.vicsjavascripts.org.uk/


// Marks form fields which fail validation with a marker

// Application Notes

// **** The Form Validation
//
// Existing Validation code requires minimal modification
// To initialise the Marker Code the form object is passed to function 'f64Mary(formobject)
// at the beginning if the validation code.
// Each field is passed to function f64MakeMark(nForm,nForm.txtLastName);
// if the field fails validation
// e.g.
// function validate(nForm){
// var isValid;
// var f64mess=[];
// f64Marks(nForm); // NOTE
// if (nForm.txtLastName.value == ""){
// f64mess.push('Please Enter a Last Name');
// f64MakeMark(nForm.txtLastName,80,-17,'Last Name'); // NOTE
// }
// if (nForm.txtFirstName.value == ""){
// f64mess.push('Please Enter a First Name');
// f64MakeMark(nForm.txtFirstName,80,-17,'First Name'); // NOTE
// }
// nButtons=nForm['optS_Class'];
// f64mess.push('You must select from: '+nButtons[0].title||nButtons[0].name);
// f64MakeMark(nButtons[0]); // NOTE
// for (i=0; i<nButtons.length; i++){
// if (nButtons[i].checked){
// f64mess.length--;
// f64RemoveMark(nForm); // NOTE
// }
// }
// nButtons=nForm['optS_Class2'];
// f64mess.push('You must select from: '+nButtons[0].title||nButtons[0].name);
// f64MakeMark(nButtons[0]); // NOTE
// for (i=0; i<nButtons.length; i++){
// if (nButtons[i].checked){
// f64mess.length--;
// f64RemoveMark(nForm); // NOTE
// }
// }
// if (f64mess.length>0){
// alert(f64mess.join('\n'));
// return false;
// }
// return true;
// }

// Function f64Marks(nForm); produces a mark o clone and removes any previously placed marks
// where
// parameter 0 = the specific form object. (object)
//
// Function f64MakeMark(nForm.formfield); produces a mark to position adjacent to the field passed to the function
// where
// parameter 0 = the specific form field object. (object)
// parameter 1 = (optional) the specific mark horizontal(X) offset from form field object. (digits, null or omit if not required)
// parameter 2 = (optional) the specific mark vertical(Y) offset from form field object. (digits, null or omit if not required)
// parameter 3 = (optional) the specific mark text (string, or omit if not required))

//
// Function f64RemoveMark(f64frm) removes last mark.
// where
// parameter 0 = the specific form object. (object)
//


// **** General

// All variable, function etc. names are prefixed with 'f64' to minimise conflicts with other JavaScripts
// These characters may be changed to characters of choice by using global find and replace.
//
// The functional code(about 1.5K) is best as an external JavaScript
//
// Tested with IE6 and Mozilla FireFox


// **** Cusomising Variables

var f64MarkColor='red'; // the Marker Text Color (string)
var f64MarkFontSize='12px'; // the Marker Text Color (string)
var f64MarkText='**'; // the Default Marker Text (string)
var f64MarkXOffset=-20; // the Default Marker Horizontal(X) offset from the field (digits)
var f64MarkYOffset=-15; // the Default Marker Vertical(Y) offset from the field (digits)


// **** Functional Code - NO NEED to Change

var f64Bdy;

function f64Marks(f64f){
f64Bdy=document.getElementsByTagName('BODY')[0];
if (!f64f.mark){
f64f.mark=document.createElement('DIV');
f64Bdy.appendChild(f64f.mark);
f64f.mark.appendChild(document.createTextNode(f64MarkText));
f64Style(f64f.mark,{position:'absolute',visibility:'hidden',color:f64MarkColor,fontSize:f64MarkFontS ize});
}
if (f64f.markary){
for (f640=0;f640<f64f.markary.length;f640++){
f64Bdy.removeChild(f64f.markary[f640]);
}
}
f64f.markary=[];
}

function f64MakeMark(f64fld,f64x,f64y,f64txt){
var f64c=f64fld.form.mark.cloneNode(true);
f64Bdy.appendChild(f64c);
f64Style(f64c,{visibility:'visible',left:(f64Pos(f64fld)[0]+(f64x||f64MarkXOffset))+'px',top:(f64Pos(f64fld)[1]+(f64y||f64MarkYOffset))+'px'});
if (f64txt){ f64c.firstChild.data=f64txt; }
f64fld.form.markary.push(f64c);
return f64c;
}

function f64RemoveMark(f64frm){
if (f64frm.markary.length<1){ return; }
f64frm.markary[f64frm.markary.length-1].parentNode.removeChild(f64frm.markary[f64frm.markary.length-1]);
f64frm.markary.length--;
}

function f64Style(f64ele,f64style){
for (key in f64style){ f64ele.style[key]=f64style[key]; }
}

function f64Pos(f64){
f64ObjLeft = f64.offsetLeft;
f64ObjTop = f64.offsetTop;
while(f64.offsetParent!=null){
f64ObjParent=f64.offsetParent;
f64ObjLeft+=f64ObjParent.offsetLeft;
f64ObjTop+=f64ObjParent.offsetTop;
f64=f64ObjParent;
}
return [f64ObjLeft,f64ObjTop];
}

//-->
</script>
</head>

<body>
<form name="form" >
<input name="email" ><br>
<input name="major" ><br>
<input name="techcourses" ><br>
<input name="age" ><br>
<input name="compability" type="radio" ><br>
<input name="compability" type="radio" ><br>
<input name="compability" type="radio" ><br>
<input name="compability" type="radio" ><br>
<input type="button" onclick="ValidateForm();" value="Test"><br>
</form>

</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum