Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2018
    Messaggi
    19

    Permettere l'inserimento di valori numerici e valori in virgola mobile in un form

    Salve a tutti, sono nuovo del forum ed è da relativamente poco che programmo in JavaScript.
    Volevo permettere l'inserimento di valori numerici e valori in virgola mobile in un form, utilizzando JavaScript. Ho provato ad utilizzare questa funzione, ma non so come settare il valore della variabile regex in modo che possano essere inseriti anche i punti. Grazie anticipatamente per la vostra risposta.
    codice:
    function onlyNumeric(evt) 
    {
        var theEvent = evt || window.event;
        var key = theEvent.keyCode || theEvent.which;
        key = String.fromCharCode(key);
        var regex = /[0-9]/;
        
        if (!regex.test(key)) {
            theEvent.returnValue = false;
            if (theEvent.preventDefault) theEvent.preventDefault();
        }
    
    }
    

  2. #2

    calle barclay

    codice:
    function onlyNumeric(evt) 
    {
        var theEvent = evt || window.event;
        var theInput=theEvent.target;
       var key = theEvent.keyCode || theEvent.which;
       key = String.fromCharCode(key);
       
        if(theInput.value.indexOf('.')==-1){// solo un punto è ammesso
            var regex = /[0-9]|\./;  //[0-9] or(|) punto (\.)
        }else{
            var regex = /[0-9]/;
        }
        
        if (!regex.test(key)) {
            theEvent.returnValue = false;
            if (theEvent.preventDefault) theEvent.preventDefault();
        }
    
    
    }
    Forse bisognerebbe considerare anche l'eventualità di inserire numeri negativi

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 © 2024 vBulletin Solutions, Inc. All rights reserved.