my dog learned polymorphism
The moose likes HTML, CSS and JavaScript and the fly likes Problem with text box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problem with text box" Watch "Problem with text box" New topic

Problem with text box

swathi veer

Joined: Nov 27, 2007
Posts: 1

I'm using struts. In one of my pages there is one text box, where we must not allow user to enter values other than numerics. For that I wrote this java script. This was working fine . The problem is , my QA found that
thay can paste some characters into that. Can any one help ....

function allowOnlyIntegers(evt) {
evt = (evt) ? evt : window.event
var c = (evt.which) ? evt.which : evt.keyCode
if (c > 31 && (c < 48 || c > 57)) {
return false
return true

Thanks in advance

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63872

Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and most people will just go elsewhere. Please read this for more information.

You can go back and change your post to add code tags by clicking the .

[Asking smart questions] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63872

I assume that the function you posted is established as a keypress event handler? This sort of detail is important to include in your posts. Please read this for more information.

If so, then of course the function will never be called unless the user is typing into the field.

So, how else can you detect when it's time to perform a field validation? What other events can you listen for in order to check the value?
[ November 27, 2007: Message edited by: Bear Bibeault ]
I agree. Here's the link:
subject: Problem with text box
It's not a secret anymore!