Html number only

what is code if you want only the number to be entered in the html text box. If you enter letters, letters will not be displayed. If you enter numbers, numbers will be displayed, please help

+4
source share
4 answers

Try the following:

<HTML> <HEAD> <SCRIPT type="text/javascript"> function isNumberKey(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } </SCRIPT> </HEAD> <BODY> <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar"> </BODY> </HTML> 
+1
source

If you want to take a chance with drafts, you can use the type of number :

 <input name="foo" type="number"> 

(See also specification )

Browser support for this is currently very limited, but increasing. You probably want to add JavaScript to simulate support in less bleeding browsers. There are various projects for this, including jsh5f and webforms2 .

Obviously, no matter what approach you take to this problem, you still need to check the data on the server side (you can never trust the client, since any restrictions that you impose can be circumvented by the user).

+3
source

You will need to use JavaScript to control this, I recommend using jQuery, and this is one of many input masking plugins: http://digitalbush.com/projects/masked-input-plugin/

+2
source

The text attribute is available for html forms. I suggest you use a server side regex so that it simply returns a number. You can use regex with javascript, but it may not be as secure as a server-side implementation.

0
source

Source: https://habr.com/ru/post/1307216/


All Articles