aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Limiting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Limiting "input type=text" to only numbers" Watch "Limiting "input type=text" to only numbers" New topic
Author

Limiting "input type=text" to only numbers

abhiavi kumar
Greenhorn

Joined: Mar 13, 2013
Posts: 20
Hi, how do I do this?

I want a text box to only be able to take numeric values. I tried this code, but it doesn't seem to work.





The error I'm getting here is as follows, If I leave "amount" empty, I get the appropriate message and works perfectly, but If I type in an alphabetic value into it, I get the alert box message Amount should only be numbers as intended, but it doesn't stop and redirects to "jackson.jsp". How do I prevent this from happening?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

It works on my computer when I reconstructed your example. (I had to add parens after the function and the text field.)

This is what I ran. It's possible your code has more in it and something else broke it. Have you looked at the JavaScript error console to see if there are any details? Or comment out extra code to see when the error goes away?



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
abhiavi kumar
Greenhorn

Joined: Mar 13, 2013
Posts: 20
Jeanne Boyarsky wrote:It works on my computer when I reconstructed your example. (I had to add parens after the function and the text field.)

This is what I ran. It's possible your code has more in it and something else broke it. Have you looked at the JavaScript error console to see if there are any details? Or comment out extra code to see when the error goes away?


Thanks a lot Jeanne. I have just basic knowledge of Javascript and I don't really know what a JavaScript error console is... or how one sees it... I used the same kind of coding in a .HTML file, and it worked for me, but this coding is from a .JSP file and it doesn't work as intended.

The error only goes away when I remove the condition to ensure only numbers are entered, i.e if I leave the "amount" text box empty, I am prompted that it is empty, and it doesn't redirect me to the page specified in the form action.

by the way, I tried a different approach, <input type="number"> and it worked for me on Google Chrome but it won't work for me on MyEclipse browser. I think this might have something to do with HTML5 (isn't number type supposed to be for HTML5?) not working on some browsers? I honestly have no idea, thank you a lot.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

abhiavi kumar wrote:Thanks a lot Jeanne. I have just basic knowledge of Javascript and I don't really know what a JavaScript error console is... or how one sees it...

It varies by browser. In Chrome, you go to view > developer > javascript console.

abhiavi kumar wrote:by the way, I tried a different approach, <input type="number"> and it worked for me on Google Chrome but it won't work for me on MyEclipse browser. I think this might have something to do with HTML5 (isn't number type supposed to be for HTML5?) not working on some browsers?

Yes. The number type was added with HTML 5. Browser support is sporadic so I wouldn't want to rely on that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Limiting "input type=text" to only numbers