Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 18
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    1,199

    una funzione che controlli che ci siano solo numeri

    Ciao tutti avrei necessità, se mi potete aiutare, di una funzione che controlli che ci siano solo numeri in 5 campi.

    Tenete anche presente che devo implementare questa funzione in una che già utilizzo e che contolla il riempimento di altri campi dello stesso form tra questi campi che sono tutti input test c'è anche una combo

    questo è il codice:

    codice:
    function checkform(){
          var myBool;
          myBool = true;
          
    
          for (var i = 0; i <= (document.forms[0].elements.length -1); i++) {
             var classe = document.forms[0].elements[i].className;
            if (classe == "Obbligatorio") {
               var nome = document.forms[0].elements[i].name;
               var id = document.forms[0].elements[i].id;
               var valore = document.forms[0].elements[i].value;   
               if (valore ==""){
                          alert ("Le champ  " + id + "  est obligatoire !");
                          document.forms[0].elements[i].focus();  
                          myBool = false;
                          break;
                }
              	if(document.forms[0].dprestazione && document.forms[0].dprestazione.type=="select-one" && document.forms[0].dprestazione.selectedIndex == 0) 
    				{ 
    				alert("Le champ prestations est obligatoire !"); 
    		          document.forms[0].dprestazione.focus();  
    				return false; 		          
    			     }else{
                       if ((id=="e-mail") && (valore !=="")){
                          var rslt = valore.match("@");
                          if (rslt == null){
                              alert ("Qu'il insère correctement le champ E-mail");
                              document.forms[0].elements[i].focus();  
                              myBool = false;
                              break;
                          }
                       }
                     }
               }     
          }
          return myBool;
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    1,199
    upppo

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    1,199
    ho trovato la funzione :

    function soloNum(cc) {
    if(cc.value.match(/^\+*\d+$/) ) {
    return true;
    } else {
    alert("solo numeri");
    cc.value = "";
    return false;
    }
    }

    e la richiamo sull' onblur dei campi che devono contenere solo numeri

    il problema però adesso e che vorrei una volta dato OK all'alert dare on focus() sul campo e come faccio ad identificare il campo su cui sono visto che i campi sono 5

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    1,199
    raga qualche aiutino per please

  5. #5
    Frontend samurai L'avatar di fcaldera
    Registrato dal
    Feb 2003
    Messaggi
    12,924
    dal codice sembrerebbe

    cc.focus()

    subito prima di return false;
    Vuoi aiutare la riforestazione responsabile?

    Iscriviti a Ecologi e inizia a rimuovere la tua impronta ecologica (30 alberi extra usando il referral)

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    1,199
    ho inserito così:

    function soloNum(cc) {
    if(cc.value.match(/^\+*\d+$/) ) {
    return true;
    } else {
    alert("Qu'il insère des seuls nombres");
    cc.value = "";
    cc.focus();
    return false;
    }
    }

    e sui miei campi che devono contenere solo numeri sull'onBlur ho inserito: soloNum(this);

    ma mi da questo problema, vado sul primo campo che deve contenere solo numeri, se inseriso lettere scatta la funzione e all'ok sull'alert mi si ripresenta l'alert??

    Perchè che sbaglio???

  7. #7

  8. #8
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    1,199
    mi da errore e mi dice che value è vuoto o non è un oggetto

  9. #9
    La richiami così?

    <input type="text" name="campo" value="" onKeyPress="return NumbersOnly(event)" />

  10. #10
    Proprio ieri ho trovato questa funzione qui sul forum e, mi funziona al 100%!!

    codice:
    <script language='javascript'>
    
    function NumbersOnly(_event, AllowDecimals, AllowNegatives, DecimalDigits)	{
    	var _source			=	(_event.srcElement)?_event.srcElement:_event.target;
    	var	_sourceText	=	_source.value;
    	var keyPressed	= _event.keyCode ? _event.keyCode : _event.which ? _event.which : _event.charCode;
    	
    	var isDigit			=	(keyPressed > 47 && keyPressed < 58);
    	
    	if (isDigit) {
    		if (AllowDecimals)	{
    			var dotPos		=	_sourceText.indexOf(".");
    			
    			if (dotPos > -1) {
    				var caretPosition	=	(document.selection)?(document.selection.createRange().getBookmark().charCodeAt(2) - 2):_source.selectionStart;
    				var _DecimalDigits	=	(DecimalDigits == null)?2:((isNaN(DecimalDigits))?2:DecimalDigits)
    			
    				if (caretPosition > dotPos)
    					return ((caretPosition - dotPos) > _DecimalDigits)?false:true;
    				else
    					return true;	
    			}
    			else
    				return true;	
    		}	
    		else
    			return true;	
    	}
    	else	{
    		if (String.fromCharCode(keyPressed) == ".")
    			if (AllowDecimals)
    				return (_sourceText.indexOf(".") == -1);
    			else
    				return false;
    
    		if (String.fromCharCode(keyPressed) == "-") {
    			if (_sourceText.charAt(0) != "-"  &&  AllowNegatives)
    				_source.value	=	"-" + _sourceText;
    				return false;
    		}
    				
    		var AllowedKeys	=	new Array(0, 8, 9, 13, 27, 46)
    				
    		for (var i=0; i<AllowedKeys.length; i++)
    			if (keyPressed == AllowedKeys[i])
    				return true;
    	}
    	
    	return false;
    
    }
    
    </script>
    Da richiamare così:

    codice:
    onKeyPress="return NumbersOnly(event)"

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.