Visualizzazione dei risultati da 1 a 10 su 10

Discussione: Validare campo ora

  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2011
    Messaggi
    207

    Validare campo ora

    Bonasera, è possibile validare un campo ora in form asp?
    vorrei fare in modo che l'ora inserita es. 8.30 venga validata, facendo in modo che l'utente inserisca l'ora ne formato 08.30.
    Grazie

  2. #2
    se vuoi una cosa interattiva usa javascript. posta nel forum apposito

  3. #3
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Sarebbe meglio chiedere il trasferimento (link segnala ad un moderatore) al moderatore di sezione, per non creare cross-post
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  4. #4

  5. #5
    Moderatore di JavaScript L'avatar di br1
    Registrato dal
    Jul 1999
    Messaggi
    19,998
    Personalmente uso questa accoppiata:
    codice:
    <script>
    function isTime(str) { 
    	if (str.length>2&&str.indexOf(":")<=0) str = str.substr(0,str.length-2)+":"+str.substr(str.length-2);
    	str="0"+str+":0"
    	spz = str.split(":"); 
    	hh = parseInt(spz[0],10)
    	mm = parseInt(spz[1],10)
    	if ((hh==24&&mm==0) || (hh<24&&mm<60)) return (hh*60+mm);
    	return false;
    } 
    function testO(obj) {
    	// verifica che sia stato introdotto un orario valido
    	obj.style.background='white'
    	RE = /[^\d^\,^\:^\.]/gi
    	obj.value=obj.value.replace(RE,"");
    	RE = /[\,\.]/gi
    	obj.value=obj.value.replace(RE,":");
    	diff = isTime(obj.value)
    	if (diff>0) {
    		hh=Math.floor(diff/60)
    		mm=(diff%60)
    		diff = String(100+hh).substr(1,2)+":"+String(100+mm).substr(1,2);
    		obj.value=diff
    	} else {
    //		obj.select()
    		if (obj.value!="" ) {
    			obj.style.background='yellow'
    			alert("Ora Errata")
    //			obj.focus()
    		} 
    	}
    }
    </script>
    
    <input type="text" onblur="testO(this)">
    Il guaio per i poveri computers e' che sono gli uomini a comandarli.

    Attenzione ai titoli delle discussioni: (ri)leggete il regolamento
    Consultate la discussione in rilievo: script / discussioni utili
    Usate la funzione di Ricerca del Forum

  6. #6
    Utente di HTML.it
    Registrato dal
    Oct 2011
    Messaggi
    207
    Grazie mille è quello che volevo..... con questo script ho risolto.
    volevo solo chiedere una cosa, se volessi aggiungere anche i secondi come dovrei modificare lo script?
    Grazie

  7. #7
    Utente di HTML.it L'avatar di vic53
    Registrato dal
    Oct 2010
    residenza
    Fonte Nuova (Roma)
    Messaggi
    592
    Cosa significa validata...?
    l'utente dopo aver inserito 8.30 nel campo "ORA" scatena un evento tipo
    1. si sposta con il TAB su un altro campo
    2. preme enter
    3 preme un tasto funzione
    4 etcc..

    in tutti questi casi intercettando l'evento, bisogna richiamare una funzione javascript che normalizza il campo inserito... mi spiego
    hai inserito 8.3 ---> la funzione traduce 08.30
    "" 8.30 ---> 08.30
    "" 008.30 ---> 08.30
    ETC ETC

    è questo che vuoi fare?
    .....
    Vic53

  8. #8
    Utente di HTML.it
    Registrato dal
    Oct 2011
    Messaggi
    207
    scusa forse mi sono spiegato male,,,,,
    questo scrtip fa già la funzione di trasformare 8.30 in 08:30
    vorrei fare in modo che mi scrivesse anche i secondi cioè
    scrivo 8.30 e mi scrivesse 08:30:00
    è possibile? e in che modo posso modificare lo script?
    premetto che non conosco javascript
    Grazie per l'aiuto.

    <script>
    function isTime(str) {
    if (str.length>2&&str.indexOf(":")<=0) str = str.substr(0,str.length-2)+":"+str.substr(str.length-2);
    str="0"+str+":0"
    spz = str.split(":");
    hh = parseInt(spz[0],10)
    mm = parseInt(spz[1],10)
    if ((hh==24&&mm==0) || (hh<24&&mm<60)) return (hh*60+mm);
    return false;
    }
    function testO(obj) {
    // verifica che sia stato introdotto un orario valido
    obj.style.background='white'
    RE = /[^\d^\,^\:^\.]/gi
    obj.value=obj.value.replace(RE,"");
    RE = /[\,\.]/gi
    obj.value=obj.value.replace(RE,":");
    diff = isTime(obj.value)
    if (diff>0) {
    hh=Math.floor(diff/60)
    mm=(diff%60)
    diff = String(100+hh).substr(1,2)+":"+String(100+mm).subs tr(1,2);
    obj.value=diff
    } else {
    // obj.select()
    if (obj.value!="" ) {
    obj.style.background='yellow'
    alert("Ora Errata")
    // obj.focus()
    }
    }
    }
    </script>

  9. #9

  10. #10
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Ci ho messo mano di fretta e devi testarla bene bene… Ti dico già che la funzione completeTime() è piuttosto minimale. Cmq… se vuoi anche i secondi puoi fare così:

    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Esempio</title>
    <script type="text/javascript">
    function checkTime (oToCheck, oKeyEvent1) {
    	var	nChar = oKeyEvent1.charCode,
    		nKey = oKeyEvent1.keyCode,
    		sVal = oToCheck.value,
    		sNewVal = sVal.substring(0, oToCheck.selectionStart - (nKey === 8 && nChar === 0 ? 1 : 0)) + (nChar > 0 ? String.fromCharCode(nChar) : "") + sVal.slice(oToCheck.selectionEnd + (nKey === 46 && nChar === 0 ? 1 : 0)), bFormat = nChar === 0 || /^\d{1,2}(?:\:\d{1,2}){0,2}$/.test(sNewVal);
    
    	if (bFormat) {
    		var aCorrect = sNewVal.split(":");
    		for (var nDigit = 0; nDigit < aCorrect.length; nDigit++) {
    			if (parseFloat(aCorrect[nDigit]) > 23 + 36 * nDigit) {
    				bFormat = false; break;
    			}
    		}
    	}
    
    	return bFormat;
    }
    
    function formatTime (oToFix, oKeyEvent2) {
    	var	nKey = oKeyEvent2.keyCode, nSelS = oToFix.selectionStart, nSelE = oToFix.selectionEnd, sVal = oToFix.value,
    		bCheck = nSelS === nSelE && nSelS === sVal.length && nKey !== 8 && nKey !== 46 && (nKey < 32 || nKey > 41);
    	if (bCheck && /^\d{1,2}$/.test(sVal) && (nSelS === 2 || parseFloat(sVal) > 2)) {
    		sVal += ":";
    		oToFix.value = sVal.replace(/^(\d)\:/, "0$1:");
    		oToFix.focus();
    	} else if (bCheck && /^\d\d\:\d/.test(sVal) && (nSelS === 5 || parseFloat(/^\d\d\:(\d)/.exec(sVal)[1]) > 5)) {
    		sVal += ":";
    		oToFix.value = sVal.replace(/^(\d\d):(\d)(\:|$)/, "$1:0$2$3");
    		oToFix.focus();
    	} else if (/\:\d$/.test(sVal) && parseFloat(/\d$/.exec(sVal)) > 5) {
    		oToFix.value = sVal.replace(/\d$/, "0$&");
    		oToFix.focus();
    	}
    }
    
    function completeTime (oToFix) {
    	oToFix.value = oToFix.value + "00:00:00".slice(oToFix.value.length);
    }
    </script>
    </head>
    
    <body>
    <form name="myForm">
    
    
    <input type="text" name="myInput" onkeypress="return checkTime(this,event);" onkeyup="formatTime(this,event);" onblur="completeTime(this);" onpaste="return false;" /></p>
    </form>
    </body>

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