Results 1 to 6 of 6

Thread: Help with a For Loop

  1. #1
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with a For Loop

    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?

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Could you up the rest of the codes
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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 == '');
    }

    }

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Up the markups (HTML)

    Quick side note:..Place your codes inside the [code] [/code ] tag of the forum
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by rangana View Post
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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:
    Code:
    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:
    Code:
    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 == '');
    }
    
    }
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •