Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript to add commas while typing

Status
Not open for further replies.

peac3

Technical User
Jan 17, 2009
226
0
0
AU
Hi guys,

I have a textbox in .net and need to be able to add commas while typing dynamically and I've tried to search online but haven't got luck, here's what I found and not working

Code:
<script type="text/javascript">
    $(document).ready(function () {
        $('[id$="jquicomma"]').keyup(function () {
            if (this.value.length % 4 == 3) this.value = this.value + ",";
        });
      });
</script>

<asp:TextBox ID="jquicomma" runat="server" Width="90%"></asp:TextBox>

anyone could help will be appreciated, Thanks!
 
not tested this but you could try something like this (note that this does not take into account whether the character is an alphanumeric or something else like a carriage return);

Code:
$(document).ready(function () {
        $('#jquicomma').keyup(function ( e ) {
			var numChars = 3; //separate every three characters
			var numCommas = $(this).text().split(',') - 1;
            if (  ($(this).text().length - numCommas) % 3 == 0){
				$(this).text( $(this).text() + ',');	
			}
        });
      });
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top