Question

I have one fields of amount, how I can deactivate the writing of points after the user to seize a point bu jquery function. You should not seize more that a point.

$('#montant-total').change(function() { 
    $( 'tr:last' ).find("td:last").text(messages['montant incorrecte']).hide();
    var inputVal = $(this).val();
    var patternMontant = /^\$?(([0-9]{0,10}))?\.[0-9]{2}$/;
    if(!patternMontant.test(inputVal)) {    
        $( 'tr:last' ).find("td:last").text(messages['montant incorrecte']).show().css("color","red");
        $(this).val('0.00');    
    }
});
$('#montant-total').keyup(function(e){
    var montantVal = $(this).val();
    point = true;
    if (montantVal.indexOf(".") >= 0){
        point = false;
    }
    if(montantVal.length == 10 && point  ){
        $(this).val(montantVal+'.');
    }   
}); 

the customer wants no plugin (maskMoney or autoNumeric) If there is a method to prevent the seizure of a point example: 156.36 instead of 1.56.56 After 10 input the system is going to seize the point automatically But before we can seize "."

Was it helpful?

Solution 2

this code wants to forbid the sizure of another dot(.) or (,), your fiels accepts only one dot(.) or (,)

$('#montant-total').keypress(function(event){

var montantVal = $(this).val();

 if (montantVal.indexOf(".") >= 0){

     var charCode = event.which;

     if (charCode == 44 || charCode == 46)  { return false;   }

     }

});

OTHER TIPS

You can use the maskMoney plugin that will do the job for you. You can get it here.

Just download the plugin, include it in your page after jQuery and do

$('#montant-total').maskMoney();

You can also pass an options object to the function to format it according to your needs.E.g.

$("#name").maskMoney({

  prefix : '$'

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top