View Full Version : Help with a For Loop
fabwebdesigns
04-19-2008, 04:58 AM
I have a questionnaire form with 12 text fields on it. The field names are Question_1, Question_2, Question_3, ... Question_12
I need to verify the fields are not empty.
Here's the script I've written but IE says undefined index:
var i;
for (i=1; i<13; i++){
if (form2.Question_i.value.length==""){
errors += '- Question ' + i + ' is required.\n';
}
}
if (errors) {
alert('The following error(s) occurred:\n'+errors );
}
else{
document.MM_returnValue = (errors == '');
}
#2
I've tried the following version and it didn't not worked either.
var i;
for (i=1; i<13; i++){
if (form2.Question_[i].value.length==""){
errors += '- Question ' + i + ' is required.\n';
}
}
#3
I've tried the following version and it didn't not worked either.
var i;
for (i=1; i<13; i++){
if (form2.Question_(i).value.length==""){
errors += '- Question ' + i + ' is required.\n';
}
}
#4
I've tried the following version and it didn't not worked either.
var i;
for (i=1; i<13; i++){
if (form2.Question_+ i +.value.length==""){
errors += '- Question ' + i + ' is required.\n';
}
}
what am i doing wrong?
rangana
04-19-2008, 05:22 AM
Could you up the rest of the codes :)
fabwebdesigns
04-19-2008, 05:30 AM
function MM_validateForm2() {
var errors;
errors='';
if (form2.Name.value.length==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Name is required.\n';
}
if (form2.Home_Phone.value.length==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Home Phone number is required.\n';
}
if (form2.Work_Phone.length==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Work Phone number is required.\n';
}
if (form2.Mobile_Phone.value.length==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Mobile Phone number is required.\n';
}
if (form2.Email_Address.value.length==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Email Address is required.\n';
}
if (form2.Last_Year_Annual_Income.value.length==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Last Year\'s Annual Income is required.\n';
}
if (form2.Licenses_Degree.value.length==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your License(s)\\Degree(s) are required.\n';
}
var i;
for (i=1; i<13; i++){
if (form2.Question_i.value.length==""){
errors += '- Question ' + i + ' is required.\n';
}
}
if (errors) {
alert('The following error(s) occurred:\n'+errors );
document.MM_returnValue = (errors == '');
}
else{
document.MM_returnValue = (errors == '');
}
}
rangana
04-19-2008, 05:35 AM
Up the markups (HTML) :)
Quick side note:..Place your codes inside the [code] [/code ] tag of the forum ;)
jscheuer1
04-20-2008, 04:51 AM
Up the markups (HTML) :)
Quick side note:..Place your codes inside the [code] [/code ] tag of the forum ;)
Better still, instead of clogging up the forum with tons of code:
Please post a link to the page on your site that contains the problematic code so we can check it out.
Well, from what I see is that your using length to get the length, but the length wouldn't equal empty space. It needs to have a value, so it'd be 0. So I'm not the best at JS, but I'm guessing that this'll work:
function MM_validateForm2() {
var errors;
errors='';
if (form2.Name.value.length=="0"){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Name is required.\n';
}
if (form2.Home_Phone.value.length=="0"){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Home Phone number is required.\n';
}
if (form2.Work_Phone.length=="0"){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Work Phone number is required.\n';
}
if (form2.Mobile_Phone.value.length=="0"){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Mobile Phone number is required.\n';
}
if (form2.Email_Address.value.length=="0"){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Email Address is required.\n';
}
if (form2.Last_Year_Annual_Income.value.length=="0"){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Last Year\'s Annual Income is required.\n';
}
if (form2.Licenses_Degree.value.length=="0"){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your License(s)\\Degree(s) are required.\n';
}
var i;
for (i=1; i<13; i++){
if (form2.Question_i.value.length=="0"){
errors += '- Question ' + i + ' is required.\n';
}
}
if (errors) {
alert('The following error(s) occurred:\n'+errors );
document.MM_returnValue = (errors == '');
}
else{
document.MM_returnValue = (errors == '');
}
}
Thats just a guess, and if that doesn't work:
function MM_validateForm2() {
var errors;
errors='';
if (form2.Name.value==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Name is required.\n';
}
if (form2.Home_Phone.value==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Home Phone number is required.\n';
}
if (form2.Work_Phone==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Work Phone number is required.\n';
}
if (form2.Mobile_Phone.value==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Mobile Phone number is required.\n';
}
if (form2.Email_Address.value==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your Email Address is required.\n';
}
if (form2.Last_Year_Annual_Income.value==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Last Year\'s Annual Income is required.\n';
}
if (form2.Licenses_Degree.value==""){
//alert("here" + form2.Q13.options[form2.Q13.selectedIndex].value);
errors += '- Your License(s)\\Degree(s) are required.\n';
}
var i;
for (i=1; i<13; i++){
if (form2.Question_i.value==""){
errors += '- Question ' + i + ' is required.\n';
}
}
if (errors) {
alert('The following error(s) occurred:\n'+errors );
document.MM_returnValue = (errors == '');
}
else{
document.MM_returnValue = (errors == '');
}
}
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.