• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Limiting "input type=text" to only numbers

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
abhiavi kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic