1

I am using the the following function in javascript.

function chknumber(a) {
    a.value = a.value.replace(/[^0-9.]/g, '', '');
}

This function replaces any non numeric character entered in a textbox on whose onkeyup i have called the above function. The problem is it allows this string as well

1..1

I want the function to replace the second dot character as well. Any suggestions will be helpful.

2 Answers 2

2

I don't advocate simplistically modifying fields while people are trying to type in them, it's just too easy to interfere with what they're doing with simple handlers like this. (Validate afterward, or use a well-written, thoroughly-tested masking library.) When you change the value of a field when the user is typing in it, you mess up where the insertion point is, which is really frustrating to the user. But...

A second replace can correct .. and such:

function chknumber(a) {
    a.value = a.value.replace(/[^0-9.]/g, '').replace(/\.{2,}/g, '.');
}

That replaces two or more . in a row with a single one. But, it would still allow 1.1.1, which you probably don't want. Sadly, JavaScript doesn't have lookbehinds, so we get into more logic:

function chknumber(a) {
    var str = a.value.replace(/[^0-9.]/g, '').replace(/\.{2,}/g, '.');
    var first, last;
    while ((first = str.indexOf(".")) !== (last = str.lastIndexOf("."))) {
        str = str.substring(0, last) + str.substring(last+1);
    }
    if (str !== a.value) {
        a.value = str;
    }
}

Can't guarantee there aren't other edge cases and such, and again, every time you assign a replacement to a.value, you're going to mess up the user's insertion point, which is surprisingly frustrating.

So, yeah: Validate afterward, or use a well-written, thoroughly-tested masking library. (I've had good luck with this jQuery plugin, if you're using jQuery.)


Side note: The second '' in your original replace is unnecessary; replace only uses two arguments.

Sign up to request clarification or add additional context in comments.

4 Comments

This is only for two or more . in a row. To match decimal number, it should replace all . after 1st occurrence.
@SanchayKumar: That's true. I was too focussed on what the question actually asked, rather than their obvious overall intent. Sadly, JavaScript doesn't have lookbehind, which would make it so simple...
@SanchayKumar: Updated it now, but I still think this is the wrong approach to start with... :-)
Thanks, that worked. And yes i agree that i should Validate afterward. so i'll call this function onblur.
1

try with match method if your input is "sajan12paul34.22" the match function will return a array contain [12 , 34.22] the array index [0] is used for getting first numeric value (12)

function chknumber(a) {
    a.value = a.value.match(/[0-9]*\.?[0-9]+/g)[0];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.