How to validate input form using Javascript to stretch user don't insert any value into input form element. Object: 1, I want user input any number value only if have some character or special character it will show errors 2, and if user let input element value as empty it also alert to user
below is my javascript code
<script type="text/javascript">
$(document).ready(function(){
$('.quantity, .unitprice').on('keyup', function(e){
var errors = array[];
var quantity = document.getElementsByClassName('.quantity').val();
var unitprice = document.getElementsByClassName('.unitprice').val();
if(quantity== ""){
errors = "quantity is empty";
}
if(unitprice== ""){
errors = "unitprice is empty";
}eles{
if(errors){
forearch(errors as errors){
alert(errors);
}
}else{
calculate();
}
}
});
function calculate(){
var qty = parseFloat( $('.quantity').val() ),
unit = parseFloat( $('.unitprice').val() );
if( isNaN(qty) || isNaN(unit) ) {
$('.result').text('');
return false;
}
var total = qty * unit;
$('.result').text(total);
}
});
</script>
And here is Html form
<td><?php echo form_input('quantity','',' class="quantity form-control"')?></td>
<td><?php echo form_input('unitprice','',' class="unitprice form-control"')?></td>
<td><label class="result"></label>$</td>
elesinstead ofelseafter your unitprice check