473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

allow only numbers in an input

PJ
I've been using this function to limit a text input to numbers only with success on an old site.

function checkForInt(evt) {
var charCode = ( evt.which ) ? evt.which : event.keyCode;
return ( charCode >= 48 && charCode <= 57 );
}<input id="txt" type="text" onkeypress="return checkForInt(event)" />However, on a new site I am building, the text input not only disallows letters, it also doesn't allow tabbing to the next control, hitting return, backspace, delete, left/right arrow, etc. I obvioulsy want to allow these keys. The only difference between the two is that the new site is xhtml 1.0 transitional. Has anyone else experienced this?

Also, what is the proper syntax to wire up an event like this without typing the event attribute in the markup itself?

document.getElementById('txt').onkeypress = function(event) { return checkForInt(event); }

????
Thanks,~PJ
Dec 19 '05 #1
5 27167
Ian
PJ wrote:
I've been using this function to limit a text input to numbers only with
success on an old site.
function checkForInt(evt) {
var charCode = ( evt.which ) ? evt.which : event.keyCode;
return ( charCode >= 48 && charCode <= 57 );
}

<input id="txt" type="text" onkeypress="return checkForInt(event)" />

However, on a new site I am building, the text input not only disallows
letters, it also doesn't allow tabbing to the next control, hitting
return, backspace, delete, left/right arrow, etc. I obvioulsy want to
allow these keys. The only difference between the two is that the new
site is xhtml 1.0 transitional. Has anyone else experienced this?
Are you saying there is an existing filter, or you are required to add one?
Also, what is the proper syntax to wire up an event like this without
typing the event attribute in the markup itself?

document.getElementById('txt').onkeypress = function(event) { return
checkForInt(event); }

Or

document.getElementById("txt").onkeypress = checkForInt;

I tend to favour this in an onload handler to putting stuff in the
markup. No point in cluttering up the XHTML with stuff that would be
ignored if the user disabled JavaScript!

Ian
Dec 19 '05 #2
PJ
I need a filter on both sites. I have modified the function to the
following now and it seems to work.

function checkForInt(evt) {
//notice that the check is != null now, as the tab key has a value of 0
var charCode = ( evt.which != null ) ? evt.which : event.keyCode
//charCodes < 32 include tab, delete, arrow keys, etc
return (charCode < 32 || (charCode >= 48 && charCode <= 57))
}

However:
document.getElementById("txt").onkeypress = checkForInt;
does not work in Firefox as an argument needs to be passed in order to pass
the event. How do you assign an event in code and pass arguments to it?

Thanks
~PJ

"Ian" <ia******@hotmail.com> wrote in message
news:11***************@drone2-svc-skyt.qsi.net.nz...
PJ wrote:
I've been using this function to limit a text input to numbers only with
success on an old site.
function checkForInt(evt) {
var charCode = ( evt.which ) ? evt.which : event.keyCode;
return ( charCode >= 48 && charCode <= 57 );
}

<input id="txt" type="text" onkeypress="return checkForInt(event)" />

However, on a new site I am building, the text input not only disallows
letters, it also doesn't allow tabbing to the next control, hitting
return, backspace, delete, left/right arrow, etc. I obvioulsy want to
allow these keys. The only difference between the two is that the new
site is xhtml 1.0 transitional. Has anyone else experienced this?
Are you saying there is an existing filter, or you are required to add

one?
Also, what is the proper syntax to wire up an event like this without
typing the event attribute in the markup itself?

document.getElementById('txt').onkeypress = function(event) { return
checkForInt(event); }

Or

document.getElementById("txt").onkeypress = checkForInt;

I tend to favour this in an onload handler to putting stuff in the
markup. No point in cluttering up the XHTML with stuff that would be
ignored if the user disabled JavaScript!

Ian

Dec 20 '05 #3
Ian
PJ wrote:
I need a filter on both sites. I have modified the function to the
following now and it seems to work.

function checkForInt(evt) {
//notice that the check is != null now, as the tab key has a value of 0
var charCode = ( evt.which != null ) ? evt.which : event.keyCode
//charCodes < 32 include tab, delete, arrow keys, etc
return (charCode < 32 || (charCode >= 48 && charCode <= 57))
}

However:
document.getElementById("txt").onkeypress = checkForInt;
does not work in Firefox as an argument needs to be passed in order to pass
the event. How do you assign an event in code and pass arguments to it?

It does.

The event is automatically passed to the function as its one and only
parameter.

In IE, you have to get the event form the window (window.event).

Ian
Dec 20 '05 #4
PJ
Ok, the reason it wasn't working for me is I had accidently left the
attribute for the event on while testing. That last function I used was no
good in IE as it throws an error that evt.which is null or not an object, as
evt itself is null in IE. The latest iteration I have is:

function checkForInt(evt) {
evt = ( evt ) ? evt : window.event;
var charCode = ( evt.which ) ? evt.which : evt.keyCode
return (charCode <= 31 || (charCode >= 48 && charCode <= 57))
}

Come to think of it, it should be titled "checkForPosInt", but oh well...

As for wiring events, I like to take the seperation a bit further and use
Ben Nolan's Behaviour file. I've found this rather nifty way that uses css
selectors. http://bennolan.com/behaviour/

var gameRules = {
'#txt' : function(element) {
element.onkeypress = checkForInt;
} ,
Behaviour.register(gameRules);

Thanks again Ian
~PJ

"Ian" <ia******@hotmail.com> wrote in message
news:11***************@drone2-svc-skyt.qsi.net.nz...
PJ wrote:
I need a filter on both sites. I have modified the function to the
following now and it seems to work.

function checkForInt(evt) {
//notice that the check is != null now, as the tab key has a value of 0 var charCode = ( evt.which != null ) ? evt.which : event.keyCode
//charCodes < 32 include tab, delete, arrow keys, etc
return (charCode < 32 || (charCode >= 48 && charCode <= 57))
}

However:
document.getElementById("txt").onkeypress = checkForInt;
does not work in Firefox as an argument needs to be passed in order to pass the event. How do you assign an event in code and pass arguments to it?

It does.

The event is automatically passed to the function as its one and only
parameter.

In IE, you have to get the event form the window (window.event).

Ian

Dec 20 '05 #5
Ian
PJ wrote:
Ok, the reason it wasn't working for me is I had accidently left the
attribute for the event on while testing. That last function I used was no
good in IE as it throws an error that evt.which is null or not an object, as
evt itself is null in IE. The latest iteration I have is:

function checkForInt(evt) {
evt = ( evt ) ? evt : window.event;
var charCode = ( evt.which ) ? evt.which : evt.keyCode
return (charCode <= 31 || (charCode >= 48 && charCode <= 57))
}

Come to think of it, it should be titled "checkForPosInt", but oh well...

As for wiring events, I like to take the seperation a bit further and use
Ben Nolan's Behaviour file. I've found this rather nifty way that uses css
selectors. http://bennolan.com/behaviour/

var gameRules = {
'#txt' : function(element) {
element.onkeypress = checkForInt;
} ,
Behaviour.register(gameRules);

Thanks again Ian
~PJ

Happy to help, I've been down this road before!

Ian
Dec 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric Lilja | last post by:
Hello, I'm working on my C++ homework and I have a question about the following function. It's part of my code the handles command line arguments. Here's the code: static void...
19
by: Eduardo Bezerra | last post by:
Hi, I'm looking for an efficient way to create the oposite of a list of numbers. For example, suppose I have this list of numbers: 100 200 300
7
by: Lars Netzel | last post by:
Hi How do I in a textbox only allow users to type in numbers and comma and dot ? they should of course be able to delete and backspace the stuff they type in too.. I figured keyDown would...
11
by: tlyczko | last post by:
Hello Rob B posted this wonderful code in another thread, http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c84d8538025980dd/6ead9d5e61be85f0#6ead9d5e61be85f0 I could not...
5
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object...
17
by: Ron | last post by:
I want to write a program that will accept a number in a textbox for example 23578 and then in a label will display the sum of the odd and even number like this... the textbox containsthe number...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
2
by: clouddragon | last post by:
Hi, i am in desperate need for any help regarding one of my assignments. I am to write a python program that lists the numbers that are composite from 1 to n(input) and write it to an external...
5
by: anumliaqat | last post by:
hello!! i hav a problem.my program is giving correct outputs for some inputs but wrong results for others. for example if u give (4 2 2)&(2 1 2) to it,it shows all results correct....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.