Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:08:13+00:00 2026-05-27T02:08:13+00:00

could someone help me please and see where I am going wrong? <script type=text/javascript>

  • 0

could someone help me please and see where I am going wrong?

<script type="text/javascript">
function validateForm(){
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("Enter Name");
  return false;
  }
var x=document.forms["myForm"]["car[]"].value;
if (x==null || x=="")
  {
  alert("Select Car");
  return false;
  }
}
  function add(tbl1) {
    var tb = document.getElementById(tbl1);
    var rowCount = tb.rows.length;
    var row = tb.insertRow(rowCount);
    var colCount = tb.rows[1].cells.length;
    for(var i=0; i<colCount; i++) {
        var newCell = row.insertCell(i);
        newCell.innerHTML = tb.rows[1].cells[i].innerHTML;
    }
  }

</script>
</head>
<body>
<form name="myForm" action="post.php" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<br><br>
<p>2. Your previous cars</p>
<table id="cars" border="1">

<tr><td width = "150"><center>Car</center></td>

</tr><tr>

</td><td><select name="car[]">
<option value="" selected=“selected”>Select one please</option>
<option value=Ford> Ford </option>
<option value=Audi> Audi </option>
<option value=Volvo> Volvo </option>
<option value=Nissan> Nissan </option>
<option value=Car 5>Car 5 </option>
</select></td>

</tr></table><br><br>
<input type="button" value="Add Row" onclick="add('cars')"/>
<input type="submit" value="Submit">
</form>

I have created an array of cars so if personA has more cars, they can click add and select another one. If they have 1 car and just enter name and 1 car, the form works but when I click Add row and select another car, it doesnt work?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:08:13+00:00Added an answer on May 27, 2026 at 2:08 am

    Your problem is that document.forms["myForm"]["car[]"] is an element when there is one select element named “car[]”, but it is an array (of elements) when there are multipel select elements named “car[]”. Your validation assumes that it is always an element, so it fails.

    You can either test if document.forms["myForm"]["car[]"] is an array, which you will likely find awkward, or you can use document.getElementsByName("car[]"), which will always return an array of elements for you to iterate over.

    The following if would evaluate to true if the array was empty or the first element didn’t meet your conditions for validity.

    var x = document.getElementsByName("car[]");
    if (x.length == 0 || x[0].value == null || x[0].value == "") { /* do stuff */ }
    

    This solution assumes that only the first must be valid (given the lack of “remove car” button, this seems most resonable; if that is not your intention, iterate over them and return false if any fails to meet the validity condition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me to see what is going wrong with this setup I
Could someone please help me with the following issue. You will see that in
Could someone please help me, I do not know why the ReadProcessMemory function fails
Could someone please help me in here, I'm just a beginner who want to
Could someone please help explain why I can't get this to work? I properly
I've recently written this with help from SO. Now could someone please tell me
I wonder whether someone could help me please, From a tutorial I found online
Could someone here please help me understand how to determine when floating point limitations
could someone please help me with this warning. I get this on my console
Please could someone help diagnose this tiny problem that shouldn't be one - trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.