I have multiple input
fields with different values. I can edit them but cannot be empty (if empty show an alert). When I am editing it to new values, if the new value matches with any of the other inputs values, I need an alert that you cannot use this.
HTML
<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">
JavaScript
$('.selector').change(function () {
alert();
});
Fiddle here
Displaying (Showing) TextBox value in Alert using jQuery Inside the jQuery document ready event handler, the Button has been assigned a jQuery Click event handler. When the Button is clicked, the TextBox value is retrieved and displayed in JavaScript Alert Box.
var $inputsWithSameValue = $('input[value=' + $(this). val() + ']'); hasDuplicates = $inputsWithSameValue.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With