This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problem with text box" Watch "Problem with text box" New topic
Author

Problem with text box

swathi veer
Greenhorn

Joined: Nov 27, 2007
Posts: 1
Hi,

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

Swathiveer
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60732
    
  65

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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with text box
 
Similar Threads
scroll bars - vertical
cross-browser javascript
How to set a default button in the form
Strange - Page Submits!!!
IE vs FF window.event issue