salve
qual'è il modo più veloce per impedire che in un campo di input l'utente digiti
i seguenti caratteri
!"£%&/()=?'^*+ç§_-><
grazie
salve
qual'è il modo più veloce per impedire che in un campo di input l'utente digiti
i seguenti caratteri
!"£%&/()=?'^*+ç§_-><
grazie
codice:<script type="text/javascript"> function normalonly(myfield, e){ if (window.event){ key = window.event.keyCode; }else if (e){ key = e.which; }else{ return true; } keychar = String.fromCharCode(key); var indesiderati='!"£%&/()=?\'^*\+ç§_-><'; //qui puoi eventualmente aggiungere altri caratteri indesiderati, facendo attenzione a non rompere la stringa if (indesiderati.indexOf(keychar)){ return true; }else{ return false; } } </script> <input onkeypress="return normalonly(this, event)" />![]()
Grazie provo subito
L'ho provata ma non va.
Mi lascia tranquillamente digitare quei caratteri speciali
Qualche suggerimento?
prova cosi:
function normalonly(myfield, e){
if (window.event){
key = e.keyCode;
}else if (e){
key = e.which;
}else{
return true;
}
keychar = String.fromCharCode(key);
var indesiderati='!"£%&/()=?\'^*+ç§_-><';
//qui puoi eventualmente aggiungere altri caratteri indesiderati, facendo attenzione a non rompere la stringa
if (indesiderati.indexOf(keychar) == -1 && key != 92){
alert("true");
return true;
}else{
alert("false");
return false;
}
}
Cosi dovrebbe funzionare... l'unico problema l'ho riscontrato sul carattere \ che purtroppo ha anche altre funzioni e all'interno della stringa da problemi... l'unico modo per risolverlo credo sia aggiungere il "char code" nel if... non ho provato tutti i caratteri... quindi fatti un po di test...
fammi sapere...codice:<script type="text/javascript"> function normalonly(myfield, e){ if (window.event){ key = window.event.keyCode; }else if (e){ key = e.which; }else{ return true; } keychar = String.fromCharCode(key); var ammessi=/[a-zA-Z0-9àèìòù]+$/; if (ammessi.test(keychar)){ return true; }else{ return false; } } </script> <input onkeypress="return normalonly(this, event)" />![]()
Perfetta!
Grazie mille