Remove leading zeros when Integer only required |
Remove leading zeros when Integer only required |
Terminator |
Sep 19 2016, 11:44 PM
Post
#1
|
Advanced Member Group: Members Posts: 218 Joined: 19-March 15 Member No.: 22,398 |
This is just for aesthetic purposes since 009 equals 9 anyway in JavaScript, but if you want to always remove leading zeros in a situation where the user must type in an integer (not decimal) is what I put below a best practice?
CODE if (num.charAt(0) == 0) { num = num.replace(/^(0+)/g, ''); $("num").value = num; } This is for programs doing calculations that require positive Integers only that must be greater than zero, like Prime Number for example. |
Christian J |
Sep 20 2016, 07:05 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,663 Joined: 10-August 06 Member No.: 7 |
Is the IF condition necessary?
|
CharlesEF |
Sep 20 2016, 09:30 AM
Post
#3
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I would use parseInt() instead.
|
Terminator |
Sep 20 2016, 02:18 PM
Post
#4
|
Advanced Member Group: Members Posts: 218 Joined: 19-March 15 Member No.: 22,398 |
The IF condition is not necessary. I am doing a RegExp to remove everything that is not a digit like below:
CODE var num = $("num").value.replace(/[^\d]/g, ''); $("num").value = num; Is it possible to combine the 0+ RegExp I have below into the 1 above? I couldn't find anything about RegEx having an AND operator. CODE num = num.replace(/^(0+)/g, ''); This post has been edited by Terminator: Sep 20 2016, 02:18 PM |
Christian J |
Sep 20 2016, 03:32 PM
Post
#5
|
. Group: WDG Moderators Posts: 9,663 Joined: 10-August 06 Member No.: 7 |
Is it possible to combine the 0+ RegExp I have below into the 1 above? There's an OR operator. Maybe something like this: CODE num = num.replace(/^(0+)|\D/g, ''); (I think \D works the same as [^\d]). Note that an input string like 0x01 will result in 01, since only the leading zero before the x is stripped. Perhaps it would be better to use two separate regex operations after all: first remove any non-digits, and only then the leading zeros. Don't know if it's possible to do that within a single regex. QUOTE I couldn't find anything about RegEx having an AND operator. I don't think there is one. Here's an idea using negations and OR to get the same effect as an AND: http://stackoverflow.com/questions/469913/...5730026#5730026 |
Terminator |
Sep 20 2016, 04:18 PM
Post
#6
|
Advanced Member Group: Members Posts: 218 Joined: 19-March 15 Member No.: 22,398 |
CODE num = num.replace(/^(0+)|\D/g, ''); That didn't work for both, it only took care of the first half. I figured it would only do one of them since it is an OR operator. For now I did them separate, it does the job correct: CODE // get number and remove non numeric characters var num = $("num").value.replace(/[^\d]/g, ''); // remove leading zeros num = num.replace(/^(0+)/g, ''); $("num").value = num; Here is a test link, the whole reason I am doing this is to practice real time/instant validation and calculations. So this program validates oninput and does not allow anything other than a digit, as well as blocking all leading zeros. And then it does the Number Factor calculations without having to hit a submit or calculate button. http://mrwdesign.net/test/js/factors/factors.html The other issue is the maxLength of 6 not working on Android Chrome, which is a well known issue for many. The link to fix this below that I found does work on my Android, but I cant seem to get that to work when used on this program. http://jsfiddle.net/f8jmB/1/ I have it commented out, but I tried this below, and it still allows more than 6 characters input on Android Chrome: CODE num.oninput = function() { if (this.value.length > 6) this.value = this.value.slice(0,6); } I tried switching my input field for num from text to number like the jsfiddle example and it still did not work. I would rather use text as input anyway. Either way I love that I learned how to instantly block unwanted characters with JavaScript, which in this program's case would be letters and symbols. This post has been edited by Terminator: Sep 20 2016, 04:21 PM |
Christian J |
Sep 20 2016, 05:05 PM
Post
#7
|
. Group: WDG Moderators Posts: 9,663 Joined: 10-August 06 Member No.: 7 |
That didn't work for both, it only took care of the first half. I figured it would only do one of them since it is an OR operator. Not sure what you mean with "both", but it works for me. E.g. the string 0x1 returns 1. QUOTE Here is a test link Too many external scripts for me to check. It's much easier with minimal test cases during development. QUOTE I tried this below, and it still allows more than 6 characters input on Android Chrome: Add alertboxes or similar to see how the browser reacts at every step. |
Lo-Fi Version | Time is now: 2nd May 2024 - 05:46 PM |