Moshiur Rahman
Moshiur Rahman

Reputation: 1642

validate table row by prevent inserting duplicate data using jquery

I am working in a form where i insert some data and added this data to table. Now i want to validate a column And this column should not be the same. enter image description here

For example i want to prevent inserting same project name in table row. How it can be done in jQuery?

Here is my html code:

<div class="form-group">
								<div class="col-sm-2" style="padding-top: 5px">
									<label>Project : </label>
								</div>
								<div class="col-sm-10" style="padding-top: 5px">
									<input type="text" class="form-control" name="project"
										id="project">
								</div>
							</div>
              <div class="form-group">
							<div class="col-md-8 col-md-offset-4" style="padding-top: 5px">
								<input type="submit" class="btn btn-lg btn-success col-md-6"
									name="add" value="ADD" id="add">
							</div>
						</div>
   <div class="col-sm-10">
			<div class="panel panel-default">
				<div >
					<table class="table" id="showtable">
					<thead>
						<tr>
							<th>ACCOUNT HEAD</th>
							<th>DEBIT</th>
							<th>CREDIT</th>
							<th>CHEQUE</th>
							<th>PROJECT NAME</th>
							<th>MR.NO</th>
							<th>DEPT NAME</th>
						</tr>
						</thead>

					</table>
				</div>
			</div>
		</div>           

Upvotes: 2

Views: 2712

Answers (1)

rdanusha
rdanusha

Reputation: 923

Use following code to check the column name is equal to input text.

add your column index to: .eq(column index)

Code Sample:

$('#showtable tbody tr').each(function () {
        $projectName = $(this).find('td:eq(4)').text();
        if ($projectName == $('#project').val()) {
            alert('ERROR')
        }
    });

Upvotes: 3

Related Questions