Reputation: 1201
I need help: I want to validate in javascript my currency inputs that are in format:
1 234,78
(so there is space between 1000 and 2 decimals that are possible).
Examples:
50,25
50
1 150
1 150,44
1 000 001
1 000 001,25
Can you help me?
Thanks2
Upvotes: 0
Views: 434
Reputation: 1298
It is tested and it is going to work
example : 9 999 999 999,99
$('#amount').change(function() {
$( 'tr:last' ).find("td:last").text(messages['amount invalid']).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['amount invalid']).show().css("color","red");
$(this).val('');
}
});
Upvotes: 1
Reputation: 1298
Use the autoNumeric plugin of jquery is very good and easy. plugin that automatically formats currency and numbers as you type on form inputs
First - include jQuery.js and autoNumeric-1.9.19.js javascript files in the header:
Second - insert a form and input field on the HTML/JSP document:
Third - in a separate script initialize autoNumeric $('selector').autoNumeric('init'):
jQuery(function($) {
$('#someID_defaults').autoNumeric('init', {aSign:',', pSign:'£', vMax:'99999.99' });
});
Upvotes: 0
Reputation: 13985
I think something like ->
re.match(r"[0-9]{0,2}(\s[0-9]{3})*", str)
should work.
Upvotes: 0