Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2001
    Messaggi
    61

    Filtro caratteri su inserimento campo testo

    Ciao,

    vorrei impedire che l'utente possa inserire più di un punto o più di una virgola in un campo di testo; al momento uso questo script che mi filtra l'input lasciando inserire solo i caratteri numerici, il backspace, il punto e la virgola ma purtroppo non impedisce l'inserimento di più virgole e punti:

    codice:
    <script language="JavaScript"><!--
    function handler(e) {
    
    	 var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    	 if (key == 8 || key == 110 || key == 188 || key == 190 || (key > 47 && key < 58) || (key > 95 && key < 107)) return true; else return false;
    
    }
    //--></script>
    
    <input type="text" name="textfield" onKeyDown="return handler(event);">
    come potrei modificarlo x ottenere il comportamento che vorrei? avete quelche idea o suggerimento?

    Ciao!

  2. #2
    Utente di HTML.it L'avatar di badaze
    Registrato dal
    Jun 2002
    residenza
    Lyon
    Messaggi
    5,372
    Penso ti puo' essere utile.

    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <script language="JavaScript"><!--
    function handler(e) {
    
    	 var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    	 if (key == 8 || key == 110 || key == 188 || key == 190 || (key > 47 && key < 58) || (key > 95 && key < 107)) return true; else return false;
    
    }
    
    function scanDigits(e) {
    var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (key == 8) {return true}
    b       = String.fromCharCode(key);
    aValue  = document.getElementById('textfield2').value + b;
    espressione = /^[0-9]+[\.|,]{0,1}[0-9]*$/;
    if (espressione.exec(aValue)!= null) {
     return true;
    }
    return false;
    }
    //--></script>
    
    <input type="text" name="textfield" onKeyDown="return handler(event);">
    
    
    <input type="text" id="textfield2" onkeypress="return scanDigits(event);">
    
    
    
    </body>
    </html>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.