I've got a form i want to apply validation to:
<form id="calculator">
<div class="f-field">
<label for="sale-price">Sale Price</label>
<input id="sale-price" type="text" value=""/>
</div>
<div class="f-field">
<label for="agent-commission">Highstreet agent commission</label>
<input id="agent-commission" type="text" value=""/>
</div>
<div class="f-field">
<label for="sale-time">Estimated sale time</label>
<input id="sale-time" type="text" value=""/>
</div>
<div class="f-field">
<input type="button" value="Calculate" id="calc-button"/>
</div>
</form>
My jQuery so far:
$(document).ready(function(){
$('#calc-button').click(function(){
var answer =
parseInt($('#sale-price').val()) /
parseInt($('#agent-commission').val()) +
parseInt($('#sale-time').val());
var validate= false;
$("#calculator input[type='text']").each(function(){
if($(this).val() != '' || $(this).attr('checked'))
validate = true;
});
if(!validate){
alert('Please select at least one filter');
return false;
}
else {
$('#calc-val').html('£' + answer);
}
});
});
Is there an easy way to setup validation on the 3 fields, which will all only ever have numeric values. I want to validate presence of a number, so anything that's not a number or lack of anything at all, will throw up a message.
I'd really want to avoid a full on jQuery plugin as i am sure there is a way for such a small form, however i am looking for advice on this.
edit Added in my jQuery, trying to work on the acceptance rate as commenter mentioned below. Sorry for not doing it sooner.