Limit user input using javascript

Can someone show me an example to limit user input (in input tag) in Javascript?

Something when we set the input (type = "text") to accept only numeric values, so it will ignore any input other than a number ...

I think this is convenient for entering a number (for example, zip code, credit card, money, cost, rating, date, etc.), and if you can show me how to create an input with a template, something like:

  Please Input Date:
 | ----------------- |
 |  /// |
 | ----------------- |

PS: I heard that WebForms 2.0 will support this in the future ... (Acid 3 compatible browser?)

input type = "date"
input type = "time"
input type = "number"
input type = "money"

But it was only news from the future: D

+6
javascript html user-interface input
source share
3 answers

It might help you.

http://www.w3schools.com/jsref/event_onkeydown.asp

<html> <body> <script type="text/javascript"> function noNumbers(e) { var keynum; var keychar; var numcheck; if(window.event) // IE { keynum = e.keyCode; } else if(e.which) // Netscape/Firefox/Opera { keynum = e.which; } keychar = String.fromCharCode(keynum); numcheck = /\d/; return !numcheck.test(keychar); } </script> <form> <input type="text" onkeydown="return noNumbers(event)" /> </form> </body> </html> 
+8
source share

This question has already been adopted, but I think this is the best solution. You do not need to implement this yourself when working with keys, etc. There are libraries for this. They call a technique that describes "input masks." Check out this jQuery plugin , it does exactly what you want.

+5
source share

You can use specific libraries such as jQuery Validate (or whatever your JS framework offers) in the submit form. Another solution is to control the meaning of the keyUp and blur events, but it can be rather messy to decide what to do with a poorly formed record (especially when the format is a bit complicated, that is, not only unwanted characters). So, overall, I would recommend client management for shipping.

+3
source share

All Articles