How to show alert for duplicated value of textbox
Updated Fiddle.
You can refresh the value attribute using $(this).attr('value', $(this).val());
first, then check if there's any other field with class selector
has the same value using value selector $('.selector[value="' + current_value + '"]').length
.
Hope this helps.
$('body').on('blur', '.selector', function () {
$(this).attr('value', $(this).val());
if ($('.selector[value="' + $(this).val() + '"]').length > 1 ) {
$(this).focus();
alert('You cannot use this');
}else if($(this).val().length == 0) {
alert('Empty value not accepted');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="selector" value="new">
<input type="text" class="selector" value="old">
<input type="text" class="selector" value="newest">
<input type="text" class="selector" value="oldest">
<input type="text" class="selector" value="older">
Try this Code:
$('.selector').on('blur',function () {
if ($('.selector[value="'+$(this).val()+'"]').not($(this)).length > 0 || current_value.length == 0 ) {
alert('duplicate value');
}
});
http://jsfiddle.net/0c0qep6y/12/