0

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!

1 Answer 1

2

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.");
}
Sign up to request clarification or add additional context in comments.

2 Comments

I was just typing up your second demo. Heads up though, you forgot return in filter().
Your second piece of code worked great. Thanks for the prompt reply!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.