How to prevent keypress two digits after a decimal number?

ekingppp

New Member
I have got a task to prevent keypress two digits after a decimal number. My jquery file is \[code\]$(function(){ $('#name').bind('paste', function(){ var self = this; setTimeout(function() { if(!/^[a-zA-Z]+$/.test($(self).val())) $(self).val(''); }, 0); }); $('#salary').bind('paste', function(){ var self = this; setTimeout(function() { if(!/^\d*(\.\d{1,2})+$/.test($(self).val())) $(self).val(''); }, 0); }); $('.decimal').keyup(function(){ var val = $(this).val(); if(isNaN(val)){ val = val.replace(/[^0-9]./g,''); if(val.split('.').length>2) val =val.replace(/\.+$/,""); } $(this).val(val); }); }); \[/code\]My html page is\[code\]<b>Name</b><input type="text" id="name" /><br/><b>Salary</b><input type="text" id="salary" class="decimal" />\[/code\]here i want only write 2 digits after decimal,how can i do this?You can see my code in http://jsfiddle.net/V6s4B/
 
Back
Top