Reputation: 423
I have a table containing a list of unique values in the second column. There is an input field to add a new row onto the field, but before inserting the row, I want to validate that the particular field value does not already exists in the table.
In my on-click event, I have tried the following:
if ($('#tablename tr > td:contains('+VALUE+')').length!=0)
{
alert("Please enter a unique row number.");
}
The problem with this is that it does not limit to a certain column, so if I enter a value that exists in any other cell of the table, it displays the alert. How can I restrict it to searching for an exact match, and only in the second column.
THanks!
Upvotes: 0
Views: 2927
Reputation: 388316
Try
if ($('#tablename tr > td:nth-child(2):contains(' + VALUE + ')').length != 0) {
alert("Please enter a unique row number.");
}
it may not be proper because of a row contains abc
and then you try to add ad
it won't allow, so try
var $tds = $('#tablename tr > td:nth-child(2)').filter(function () {
return $.trim($(this).text()) == VALUE;
});
if ($tds.length != 0) {
alert("Please enter a unique row number.");
}
Upvotes: 2