What I’m trying to achieve is (using onkeyup):
If someone enters a non-integer character anywhere in the string, it is removed.
If someone enters a zero, nothing else is accepted (but the single zero stays).
Any and all integers are allowed.
Examples:
0000 cannot be entered because everything after the first 0 is removed and stays as ‘0’
0ad7b0rt_x becomes 0 (see above)
asdf becomes blank
1000000000 is accepted
10000asdf0 cannot be entered because ‘asdf’ will be removed, so this becomes 100000 (the current regex will do this)
@AllanP, unless I am missing something, I think your regex will strip all leading zeroes, I believe not allowing an initial single zero to be entered. This is for a monetary value input, and the user should be allowed to say “zero dollars”. I just don’t want some joker to enter “00” or “0.00”.
@felgall, thank you; I’ll give that a shot and report back.
V/r,
UPDATE:@felgall’s regex (as entered) will allow a single integer and nothing else. If 1 through 9 is entered, it will stay and everything else after it will be removed. If a 0 is entered, any non-integer entered after it will be removed, but any other integer will replace the 0, and everything entered after will be replaced. Much appreciated, but it is not producing the desired results.
Slightly hackish, but it is producing the desired results. If anyone can think of how to slim this down to a single replace() instead of two, please LMK.
From what I understand, the rules are no non-digits and no leading 0’s, right?
.replace(/\D|^0+/g, '')
That being said, as a user, I’m very much not a fan of my input being changed on every keystroke. On occasion, I goof and enter a value awkwardly, and munging my value while I’m still trying to enter it is frustrating. Wait until I say I’m ready.
Appreciated, but I’d rather not parseFloat(), Number(), or do anything else to strip out currency symbols, commas, or decimals. Simple integers, that’s all.
For readability, I think I would implement this as two separate RegExes.
But as noted above, try not to be too aggressive with it - if possible, I would allow the user to input anything they like, and then interpret it later on.
Here’s a specific and real-life example of how it could go down. Let’s say I want to enter 101, but maybe my keyboard sticks, or maybe I whiffed the key, and I miss the first “1”, so instead I end up typing “01”, which would actually come out as just 0 with your rules. “Oops,” I say, and I try to fix it. I type one of the missing 1’s… nothing happens. Okay… I click to move the cursor before the zero, and type a “1”… nothing happens. Now I’m thinking, “What the f—k is wrong with this website?!”
Already tested. Works as desired. If a 0 is entered, first, and the cursor moved to before the 0 and any other number is entered, it enters before the zero - action is on keyup, not keydown.
As far as pasting a value in, I haven’t tried, yet, but I am using the onpaste attribute, as well as the onblur, so even if it is pasted in with an undesired value, the blur will fix it.
These regex’s are getting highly complicated and should never be used in production.
What’s wrong with using a simple if/else condition?
If (this.value = "0") {
// allow zero to remain
} else {
// do your regex here
}
Keep it simple, for with complex code the ability to understand it fades with time, and checking later on if there are any bugs becomes near-impossible.
You need to split the problem into three sections:
Test if the string is a number
If it is a number strip the leading unwanted characters
test if the stripped result length is zero. if it is the original string was only zeros and can be written back as"0"
Here is the script to do it.
<script type="text/javascript">
var test="001234"; // <<<<<<<<<<<<<<<<<<< CHANGE THIS TO TEST
var result, regexp;
if(isNaN(test)===true) {result="Not a number"; }
else{ // Is a number so strip leading chars
regexp=/^[0|\D]*/g;
result=test.replace(regexp,'');
}
// check for only zeros
if(result===""){result="0"; }
document.write("test= "+test+" result= >"+result+"<");
</script>