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
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
se vuoi una cosa interattiva usa javascript. posta nel forum apposito
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
Spostata in js.
Roby
www.creamweb.it [v. 3.0]
:: Script ASP!
:: Web directory gratuita!
:: Campioni del mondo!
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
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
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
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>
C'è anche questa soluzione…: http://forum.html.it/forum/showthrea...9#post13394101![]()
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>![]()