I am writing a jquery function to check if user input contains letters instead of numbers. Im just trying to get the basic functionality working and I am using an alert box to test this. The test should be displaying the alert regardless of what is entered. It works when numbers are entered, but any other character entered does not fire the alert,. Any ideas what is going on here?
The form is broken into fieldsets contained in divs, which pop-up as overlays. The function is called whenever the user tries to close a pop-up window,. Not that this should cause a problem?
Here is the test function:
function validateItem(item){
var fvalue = $(item).val();
alert(fvalue);
return false; }
Here is a sample table item
<tr>
<td class="td_thumbs">
<div>
<a class="fancybox" data-fancybox-group="vinyl-banners" href="img/products/vinyl-corporateblue.jpg"> <img src="img/products/thumbs/vinyl-corporateblue-thumb.png" alt="vinyl c-blue"/></a>
<label>Corporate Blue</label>
</div>
</td>
<td>6</td>
<td>
<input id="vinyl-blue" type="text" max="6" min="0" value="0"/>
</td>
</tr>
The calling function,
//iterate through each table
$('table').each(function() {
$thisTable = $(this);
//iterate through each input
$(this).find('input').each(function() {
var $this = $(this), i_value = $this.attr('value'),
itemLabel = $this.closest('tr').find('label').html();
//if any items have a positive value
if (i_value > 0) {
//we want to grab the heading for these items
setHeading = true;
//validate item
if(validateItem($this)){//if quantities are valid
//build list of items of this type
$this.css({
'border':'1px solid #ddd',
'background-color' : '#efefef'
});
items += '<li>' + i_value + 'x ' + itemLabel + '</li>';
}
else{//invalid quantity
$this.css({
'border':'1px dashed #F00',
'background-color' : '#f7d2d4'
});
errorFlag = true;
//exit item and table loops
return false;
}//end validate
}//end value check
});//end input iteration
Found the issue! The function was called within the following..
which meant it was only called when a number was input. I placed the call outside of this if statement and it works as expected, so the solution was to change
to