Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple JQuery Numeric #64

Open
Zolfaghari opened this issue Mar 10, 2014 · 0 comments
Open

Simple JQuery Numeric #64

Zolfaghari opened this issue Mar 10, 2014 · 0 comments

Comments

@Zolfaghari
Copy link

Hi dear,
Now I using bellow script for format numbers.
it is small and better than ... tested.
with special thanks to my dear friend Mr.Ali Mostafavi.

using example:

.numeric cssClass for just numbers.
.numericAmount cssClass for numbers with thousands separator.
.numericAmountDecimal cssClass for numbers with decimal and thousands separator.

<script tag start ``` $(document).ready(function () { $(".numeric").on('keyup', function () { this.value = this.value.replace(/[^0-9]/, ''); $(this).val(this.value); }); $(".numericAmount").on('keyup', function () { this.value = this.value.replace(/[^0-9]/g, ''); $(this).val(ReplaceNumberWithCommas(this.value)); }); $(".numericAmountDecimal").on('keypress', function (e) { var nkeycode = e.charCode || e.keyCode; var chDecimalDot = String.fromCharCode(nkeycode).toLowerCase(); if (chDecimalDot === ('.').toLowerCase()) { var dotArray = this.value.toString().split('.'); if (dotArray.length > 1) { return false; } else if (this.value == 0) { this.value = '0.'; return false; } } }); $(".numericAmountDecimal").on('keyup', function (e) { this.value = this.value.replace(/[^0-9\.]/g, ''); $(this).val(ReplaceNumberWithCommas(this.value)); }); }); function ReplaceNumberWithCommas(yourNumber) { //Separates the components of the number var n = yourNumber.toString().split("."); //Comma-fies the first part n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); //Combines the two sections return n.join("."); } ``` script tag end>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant