The following code works:
$(".valClear").each(function () {
if ($(this).val().indexOf(".png") == -1) {
$(this).val('');
}
});
However this doesn’t (the value is removed even if it contains .png or .jpeg), what am I doing wrong?
$(".valClear").each(function () {
if (($(this).val().indexOf(".png") == -1) || ($(this).val().indexOf(".jpeg") == -1)) {
$(this).val('');
}
});
You’re doing an
OR, seems like you want anAND, like this:With your
ORlogic, the value will likely not have either one of them, and the other part will be true, you want anAND, meaning it has neither in the value.You can also optimize it a bit, like this:
To stray quite a bit from the question, and towards your actual problem…I’d actually change up how you’re doing this, using an array of allowed extensions and
$.inArray()to check it, like this:You can test it here. You can see from the format, this is much easier to expand to allow additional extensions later.