goodk
asked on
Form validation
wanted to look up inputs only inside a certain TR and TD ids. How can I do that? thanks
function validateInput(xTable) {
var isValid = true;
var tblElement = document.getElementById(xTable),
tblTrElement = tblElement.getElementById("TRNewRecord"),
txtElements = tblTrElement.getElementsByTagName('input');
for (var i = 0; i < txtElements.length; i++) {
if (!isValid) break;
var item = txtElements[i],
val = item.value;
// alert(item);
alert(val);
// do your validation here
// isValid = (val >= 100);
isValid = isNumber(val);
alert(" isvalid= " + isValid)
}
if (isValid) {
// do something or do nothing, form will be submitted
alert('All good, form will be submitted');
} else {
// show some kind of error to the user
alert('Some values are lower than 100');
}
return isValid;
}
<table id="mytable">
<tr id ="TRNewRecord">
<td>
Row 1
</td>
<td>
<div id="div1">
<input type="text" name="one" />
</div>
</td>
</tr>
<tr>
<td>
Row 1
</td>
<td>
<div id="div2">
<input type="text" name="two" />
</div>
</td>
</tr>
</table>
ASKER
would it be ok if there are more more than one tables on the form. I only change the table name. Do i need to append the table name for the TR records also?
ASKER
Ok, let me ask my question a different way.
Can we iterate through this for a specified cell for a particular table and a particular input name?
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Click the button to return the number of cells in the table's first row.</p>
<table id="myTable">
<tr>
<td><input type="text" name="name"></td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("m yTable").r ows[0].cel ls.length;
document.getElementById("d emo").inne rHTML = "Found " + x + " cells in the first tr element.";
}
</script>
</body>
</html>
Can we iterate through this for a specified cell for a particular table and a particular input name?
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Click the button to return the number of cells in the table's first row.</p>
<table id="myTable">
<tr>
<td><input type="text" name="name"></td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("m
document.getElementById("d
}
</script>
</body>
</html>
>> Can we iterate through this for a specified cell for a particular table
Yes. Just make sure the id is unique throughout the document.
>>and a particular input name?
Once you have a reference to the cell that contains what you are interested in, you can "search" within that cell --ex:
// should give you an "array" of all the <input name="age"/> within the first cell of the first row.
var names = document.getElementById("m yTable").r ows[0].cel ls[0].getE lementsByT agName('ag e');
//since there is only one named age in that cell, we use index 0
alert( names[0].value );
Yes. Just make sure the id is unique throughout the document.
>>and a particular input name?
Once you have a reference to the cell that contains what you are interested in, you can "search" within that cell --ex:
// should give you an "array" of all the <input name="age"/> within the first cell of the first row.
var names = document.getElementById("m
//since there is only one named age in that cell, we use index 0
alert( names[0].value );
ASKER
is this right? not working?
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Click the button to return the number of cells in the table's first row.</p>
<table id="myTable">
<tr>
<td><input type="text" name="age"></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var names = document.getElementById("m yTable").r ows[0].cel ls[0].getE lementsByT agName('ag e');
document.getElementById("d emo").inne rHTML= names[0].value ;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Click the button to return the number of cells in the table's first row.</p>
<table id="myTable">
<tr>
<td><input type="text" name="age"></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button onclick="myFunction()">Try
<p id="demo"></p>
<script>
function myFunction() {
var names = document.getElementById("m
document.getElementById("d
}
</script>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much for your help.
ASKER
In reality this is what I was trying to get. Now I have full control over the input fields
var RowOneCellCount = document.getElementById("myTable").rows[0].cells.length;
alert("CellCount=" + RowOneCellCount);
for (var j = 0; j < RowOneCellCount; ++j) {
var inputs = document.getElementById("myTable").rows[0].cells[j].getElementsByTagName('input');
alert(inputs[0].name);
alert(inputs[0].value);
}
ASKER
How do I test if input does not exists?
if(??? inputs )
if(??? inputs )
Open in new window
The javascript code at the end will help you validate the <input> elements only in the first row (read the comments in the code). However, if you want to traverse through all the <TR> elements, what you need to do is:
Open in new window
Here's a modified version of your posted code:
Open in new window
Regards,Hielo