Numeric and Decimal only Entries: can this code do that?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Numeric and Decimal only Entries: can this code do that?

Post by simonmlewis »

Code: Select all

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
This code works by allowing only a numeric value to be entered, but I need to be able to add something like 2.5 as well. So can it be adjusted so a . can be entered?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Numeric and Decimal only Entries: can this code do that?

Post by Celauran »

This is restricting the actual key codes, which seems like a strange approach. On my keyboard, period is 110 and decimal (on the numpad) is 190 if you want to continue down that road.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Numeric and Decimal only Entries: can this code do that?

Post by simonmlewis »

Sorry? If there is a simpler way of doing it - so I can enter only numeric and decimal point... ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Numeric and Decimal only Entries: can this code do that?

Post by Christopher »

Google "javascript verify if string is numeric" to get some ideas. You should verify the value after input anyway.

And, if you are going to check keypresses, then you may need to track whether the user type '.' twice. You need to only allow '.NN', 'NN.NN' and 'NN.' , not something like '.NN.NN.'
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Numeric and Decimal only Entries: can this code do that?

Post by simonmlewis »

Very good point. They are unlikely to do that. It's mainly so they don't enter 2.5", but only enter 2.5. So if that was the only real concern, how could i adjust my code so that it takes 0-9 and . ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Numeric and Decimal only Entries: can this code do that?

Post by Celauran »

The bigger point here is to validate the data they have entered rather than trying to restrict what they can enter. 222.555.22.5.25 is no more valid than 2.5"
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Numeric and Decimal only Entries: can this code do that?

Post by simonmlewis »

Yes I know, but these people know what to enter. I just want to stop them entering ". They know to enter 2.5, or 7. But they could easily do 2.5".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Numeric and Decimal only Entries: can this code do that?

Post by simonmlewis »

How would i filter out a " in code after submission? So if they did 2.5", how would i check for it and remove?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Numeric and Decimal only Entries: can this code do that?

Post by simonmlewis »

Ignore me, it's easy:

Code: Select all

$depth = str_replace("\"", "", $depth);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Numeric and Decimal only Entries: can this code do that?

Post by Christopher »

Or more thorough:

Code: Select all

$depth = preg_replace('/[^0-9\.]/', '', $depth);
(#10850)
Post Reply