jquery - Duplicate input in form -
i have dynamically generated form multiple text input fields:
<input class="w200" type="text" value="" name="field[7018]"> <input class="w200" type="text" value="" name="field[7019]"> <input class="w200" type="text" value="" name="field[7020]"> ... <input class="w200" type="text" value="" name="field[7055]">
using jquery, how can detect duplicate values on input?
there's post on so proposes following solution:
$("#check").click(function() { $.post("checkname.php", { name : $("#textboxname").val() }, function(data) { //data contain output file `checkname.php` if(data=="ok") { //imagine case output ok if not duplicate found alert('ok'); else { alert('duplicate name exists'); } ); });
it works if use enters 1 value @ time. if user gets opens form prepopulated values, edit them. how check duplicates then?
if asking how check duplicate values of existing input-elements on page, :
$("input[type='text']").change( function() { // check input ($(this).val()) validity here var valueofchangedinput = $(this).val(); var timerepeated = 0; $("input[type='text']").each(function () { //inside each() check 'valueofchangedinput' other existing input if ($(this).val() == valueofchangedinput ) { timerepeated++; //this executed @ least 1 time because of input, changed } }); if(timerepeated > 1) { alert("duplicate value found !"); } else { alert("no duplicates !"); } });
Comments
Post a Comment