JQuery : Text field as Currency field with Sample Code

The currency field only accepts numeric values with decimal point of two digits. Below is the JQuery code to validate normal textfield as Currency field, will accepts only numbers and dot followed by two digits.

Sample Code 

 

$("input.form-text").keypress(function (event) {
	if ((event.which != 46 || $(this).val().indexOf('.') != -1) &&
		((event.which < 48 || event.which > 57) &&
			(event.which != 0 && event.which != 8))) {
		event.preventDefault();
	}
 
	var text = $(this).val();
 
	if ((text.indexOf('.') != -1) &&
		(text.substring(text.indexOf('.')).length > 2) &&
		(event.which != 0 && event.which != 8) &&
		($(this)[0].selectionStart >= text.length - 2)) {
		event.preventDefault();
	}
});

 

Category: