{"id":1071,"date":"2012-03-22T00:01:54","date_gmt":"2012-03-21T23:01:54","guid":{"rendered":"http:\/\/www.manuelmarangoni.it\/sir-bit\/?p=1071"},"modified":"2016-01-22T17:54:07","modified_gmt":"2016-01-22T16:54:07","slug":"javascript-jquery-impedire-di-digitare-determinati-caratteri-nellinput-controllo-in-tempo-reale","status":"publish","type":"post","link":"https:\/\/www.manuelmarangoni.it\/sir-bit\/1071\/javascript-jquery-impedire-di-digitare-determinati-caratteri-nellinput-controllo-in-tempo-reale\/","title":{"rendered":"Javascript, Jquery &#8211; Impedire di digitare determinati caratteri nell&#8217;input (controllo in tempo reale)"},"content":{"rendered":"<p>All&#8217;interno di un form, in un campo di input o in una textarea, pu\u00f2 capitare di dover &#8220;obbligare&#8221; l&#8217;utente a inserire solo numeri o solo determinati caratteri. E&#8217; utile, tra l&#8217;altro, come controllo preventivo: all&#8217;invio del form non dovremmo controllare se il valore corrisponde al nostro standard voluto, perch\u00e9 l&#8217;utente sar\u00e0 stato costretto a inserirlo correttamente (per sicurezza, a ogni modo, io consiglio sempre un controllo alla ricezione&#8230; male non fa).<\/p>\n<p>In questo articolo vedremo un esempio su <strong>come limitare la digitazione dell&#8217;utente ai soli caratteri numerici<\/strong>. Potrete naturalmente modificarlo per adattarlo ai vostri scopi.<br \/>\nLo script da scaricare \u00e8 completamente funzionante cos\u00ec com&#8217;\u00e8. All&#8217;interno troverete il codice in due versioni: in javascript puro e con l&#8217;aggiunta di una funzione jquery. Per quest&#8217;ultimo esempio troverete anche il file <code>jquery.js<\/code> da includere obbligatoriamente per il corretto funzionamento.<br \/>\nIl codice \u00e8 semplice e non richiede grandi spiegazioni. Diamo comunque un&#8217;occhiata alla sua struttura.<\/p>\n\n        <h2>Download script<\/h2>\n        <div style=\"float:left; width:100%; margin-top:60px; margin-bottom:40px;\">\n        <div style=\"float:left; width:100px;\"><img decoding=\"async\" title=\"[JAVASCRIPT, JQUERY] Bloccare in tempo reale la digitazione di caratteri nell&#8217;input\" src=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-content\/themes\/sirbit\/images\/redirect_icon.png\" alt=\"[JAVASCRIPT, JQUERY] Bloccare in tempo reale la digitazione di caratteri nell&#8217;input\" \/><\/div>\n        <div style=\"float:left; width:350px; padding-top:30px; font-size:14px;\"><strong><a title=\"[JAVASCRIPT, JQUERY] Bloccare in tempo reale la digitazione di caratteri nell&#8217;input\" href=\"https:\/\/www.manuelmarangoni.it\/sir-bit\/code\/javascript-jquery-bloccare-in-tempo-reale-la-digitazione-di-caratteri-nellinput\/\">Pagina diretta alla scheda e al download del codice<\/a><\/strong><\/div>\n        <\/div>\n        <div style=\"clear:both;\"> <\/div>\n<h2>Il form e la funzione di base<\/h2>\n<p>Nella sua forma base, troviamo questa funzione javascript da includere nella pagina:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"javascript\" style=\"font-family: monospace;\"><span style=\"color: #003366; font-weight: bold;\">function<\/span> onlyNumeric<span style=\"color: #009900;\">(<\/span>evt<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n   <span style=\"color: #006600; font-style: italic;\">\/*Questa condizione ternaria \u00e8 necessaria per questioni di compatibilit\u00e0 tra browser se \"evt.which\" non viene preso, usa \"event.keyCode\" *\/<\/span>\r\n   <span style=\"color: #003366; font-weight: bold;\">var<\/span> charCode<span style=\"color: #339933;\">=<\/span><span style=\"color: #009900;\">(<\/span>evt.<span style=\"color: #660066;\">which<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">?<\/span>evt.<span style=\"color: #660066;\">which<\/span><span style=\"color: #339933;\">:<\/span>event.<span style=\"color: #660066;\">keyCode<\/span><span style=\"color: #339933;\">;<\/span>\r\n   <span style=\"color: #000066; font-weight: bold;\">if<\/span><span style=\"color: #009900;\">(<\/span>charCode<span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #cc0000;\">31<\/span> <span style=\"color: #339933;\">&amp;&amp;<\/span> <span style=\"color: #009900;\">(<\/span>charCode<span style=\"color: #339933;\">&lt;<\/span><span style=\"color: #cc0000;\">48<\/span> <span style=\"color: #339933;\">||<\/span> charCode<span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #cc0000;\">57<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n      <span style=\"color: #000066; font-weight: bold;\">return<\/span> <span style=\"color: #003366; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">;<\/span>\r\n   <span style=\"color: #000066; font-weight: bold;\">return<\/span> <span style=\"color: #003366; font-weight: bold;\">true<\/span><span style=\"color: #339933;\">;<\/span>\r\n<span style=\"color: #009900;\">}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Il codice specifica che alla pressione di un qualsiasi tasto della tastiera sar\u00e0 evocata la funzione <code>onlyNumeric()<\/code>. Il controllo del carattere digitato viene fatto <strong>considerando il suo valore nel codice ASCII<\/strong> (la tabella del codice ASCII potete trovarla facilmente con una ricerca su internet; a <a title=\"Tabella di codice ASCII\" href=\"http:\/\/www.oppo.it\/tabelle\/tabella_ascii.htm\" target=\"_blank\">questo sito<\/a> trovate un esempio). Nel concreto, si prendono tutti i valori inferiori da 0 a 30 (tra cui troviamo il tasto di <em>backspace<\/em>, senza il quale l&#8217;utente non potrebbe cancellare) e da 48 a 57 (le cifre numeriche).<!--more--><\/p>\n<p>Il form non ha nulla di particolare, se non l&#8217;aggiunta nel campo di input dell&#8217;attributo <code>onkeypress<\/code>: indica per l&#8217;appunto di avviare la funzione <code>onlyNumeric()<\/code> alla pressione di un tasto della tastiera:<\/p>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"html4strict\" style=\"font-family: monospace;\"><span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">form<\/span> <span style=\"color: #000066;\">method<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"post\"<\/span> <span style=\"color: #000066;\">action<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"\"<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"form_check\"<\/span>&gt;<\/span>\r\n\tInserisci un testo (qualsiasi carattere non numerico sar\u00e0 bloccato)<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n\t<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">input<\/span> <span style=\"color: #000066;\">type<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"text\"<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"solo_numeri\"<\/span> <span style=\"color: #000066;\">value<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"\"<\/span> <span style=\"color: #000066;\">onkeypress<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"return onlyNumeric(event);\"<\/span>&gt;<\/span>\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">form<\/span>&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>L&#8217;uso del jquery come miglioramento<\/h2>\n<p>Grazie all&#8217;uso del JQuery, possiamo migliorare il codice precedente e <strong>applicare la funzione <code>onlyNumeric()<\/code> esclusivamente a quegli input che presentano una determinata classe css<\/strong> (che nel nostro caso chiameremo <code>onlyNumber<\/code>). Il vantaggio \u00e8 che non occorre l&#8217;aggiunta dell&#8217;attributo <code>onkeypress<\/code> che abbiamo visto prima (soprattutto contando che, in genere, gli input vengono comunque formattati con delle classi). Inoltre, dal mio punto di vista, il codice risulta pi\u00f9 pulito e immediato.<br \/>\nEcco il codice modificato e il form relativo:<\/p>\n\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"javascript\" style=\"font-family: monospace;\"><span style=\"color: #006600; font-style: italic;\">\/* In questo caso la funzione javascript si applica solo agli input con classe \"onlyNumber\" *\/<\/span>\r\njQuery<span style=\"color: #009900;\">(<\/span>document<span style=\"color: #009900;\">)<\/span>.<span style=\"color: #660066;\">ready<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #003366; font-weight: bold;\">function<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">{<\/span>\r\n\t$<span style=\"color: #009900;\">(<\/span><span style=\"color: #3366cc;\">\".onlyNumber\"<\/span><span style=\"color: #009900;\">)<\/span>.<span style=\"color: #660066;\">keypress<\/span><span style=\"color: #009900;\">(<\/span><span style=\"color: #003366; font-weight: bold;\">function<\/span><span style=\"color: #009900;\">(<\/span>evt<span style=\"color: #009900;\">)<\/span> <span style=\"color: #009900;\">{<\/span>\r\n\t   <span style=\"color: #003366; font-weight: bold;\">var<\/span> charCode<span style=\"color: #339933;\">=<\/span><span style=\"color: #009900;\">(<\/span>evt.<span style=\"color: #660066;\">which<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">?<\/span>evt.<span style=\"color: #660066;\">which<\/span><span style=\"color: #339933;\">:<\/span>event.<span style=\"color: #660066;\">keyCode<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t   <span style=\"color: #000066; font-weight: bold;\">if<\/span> <span style=\"color: #009900;\">(<\/span>charCode<span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #cc0000;\">31<\/span> <span style=\"color: #339933;\">&amp;&amp;<\/span> <span style=\"color: #009900;\">(<\/span>charCode<span style=\"color: #339933;\">&lt;<\/span><span style=\"color: #cc0000;\">48<\/span> <span style=\"color: #339933;\">||<\/span> charCode<span style=\"color: #339933;\">&gt;<\/span><span style=\"color: #cc0000;\">57<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\t\t  <span style=\"color: #000066; font-weight: bold;\">return<\/span> <span style=\"color: #003366; font-weight: bold;\">false<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t   <span style=\"color: #000066; font-weight: bold;\">return<\/span> <span style=\"color: #003366; font-weight: bold;\">true<\/span><span style=\"color: #339933;\">;<\/span>\r\n\t<span style=\"color: #009900;\">}<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span>\r\n<span style=\"color: #009900;\">}<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"wp_syntax\">\n<table>\n<tbody>\n<tr>\n<td class=\"line_numbers\">\n<pre>1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8<\/pre>\n<\/td>\n<td class=\"code\">\n<pre class=\"html4strict\" style=\"font-family: monospace;\"><span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">form<\/span> <span style=\"color: #000066;\">method<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"post\"<\/span> <span style=\"color: #000066;\">action<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"\"<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"form_check\"<\/span>&gt;<\/span>\r\n\tInserisci un testo (qualsiasi carattere non numerico sar\u00e0 bloccato)<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n\t<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">input<\/span> <span style=\"color: #000066;\">type<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"text\"<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"solo_numeri\"<\/span> <span style=\"color: #000066;\">value<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"\"<\/span> <span style=\"color: #000066;\">class<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"onlyNumber\"<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n\t<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n\r\n\tInserisci un testo (in questo caso sono validi anche caratteri, perch\u00e9 l'input non ha la classe \"onlyNumber\")<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">br<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n\t<span style=\"color: #009900;\">&lt;<span style=\"color: #000000; font-weight: bold;\">input<\/span> <span style=\"color: #000066;\">type<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"text\"<\/span> <span style=\"color: #000066;\">name<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"solo_numeri2\"<\/span> <span style=\"color: #000066;\">value<\/span><span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff0000;\">\"\"<\/span> <span style=\"color: #66cc66;\">\/<\/span>&gt;<\/span>\r\n<span style=\"color: #009900;\">&lt;<span style=\"color: #66cc66;\">\/<\/span><span style=\"color: #000000; font-weight: bold;\">form<\/span>&gt;<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Uno script liberamente scaricabile che mostra come limitare l&#8217;inserimento dell&#8217;utente di numeri o stringhe in un input. Utile per il controllo di un form.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[12,11,6],"class_list":["post-1071","post","type-post","status-publish","format-standard","hentry","category-javascript-e-derivati","tag-javascript","tag-jquery","tag-script","cat-80-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1071"}],"collection":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/comments?post=1071"}],"version-history":[{"count":13,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1071\/revisions"}],"predecessor-version":[{"id":2300,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/posts\/1071\/revisions\/2300"}],"wp:attachment":[{"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/media?parent=1071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/categories?post=1071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.manuelmarangoni.it\/sir-bit\/wp-json\/wp\/v2\/tags?post=1071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}