286 lines
6.9 KiB
JavaScript
286 lines
6.9 KiB
JavaScript
/*ver. 1.4
|
|
23-02-2009 gestito date dal 1800
|
|
15-10-2008 gestito cr su campu data con submit
|
|
gestito inpubt 10/10/ e 10/10/08
|
|
gestito firefox
|
|
11-10-2008 formatTime
|
|
|
|
*/
|
|
|
|
<!-- This script has been in the http://www.javascripts.com Javascript Public Library! -->
|
|
<!-- Note that though this material may have been in a public depository, certain author copyright restrictions may apply. -->
|
|
|
|
/***********************************************************************************************************************************
|
|
CUSTOM DATE FUNCTIONS WRITTEN BY Dest@pobox.com
|
|
FormatDate()- AUTOINSERTS the /'s IN A DATE
|
|
CheckDate()- Validates Date Entered is a Valid Date
|
|
|
|
COPY AND PASTE THE FOLLOWING INTO YOUR INPUT FIELD AND CHANGE TO APPROPRIATE FRAME LOCATION (ie: parent. or self.) :
|
|
onchange="checkDate(this)" onkeydown="formatDate(this, window.event.keyCode,'down')" onkeyup="formatDate(this, window.event.keyCode,'up')"
|
|
<INPUT TYPE=text onChange="checkDate(this);printDate(this,'giorno')" onkeydown="formatDate(this, window.event.keyCode,'down')" onkeyup="formatDate(this, window.event.keyCode,'up')">
|
|
aggiunto printDate. Devo mettere un field che si chiama giorno dove facco apparire il giorno della settimana
|
|
per formatTime l'utilizzo è lo stesso. Vedi function.js per automatismo passaggio di campo
|
|
SCORCIATOIE: <%=Ab.jsDateInput(nextFocus,printDateField)%> <%=Ab.jsDateInput(nextFocus)%> <%=Ab.jsDateInput()%>
|
|
***********************************************************************************************************************************/
|
|
function formatDate(i, delKey,direction) {
|
|
if (i.value.length < 10) {
|
|
if (delKey!=9) { //tab
|
|
if(delKey!=8 && delKey!=46 && delKey!=16 && !(delKey>36 && delKey<41)){ //if the delete, backspace, shift, are not the keys that caused the keyup event.
|
|
var fieldLen = i.value.length
|
|
if ((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <=105)) {
|
|
if (fieldLen == 2 || fieldLen == 5) {
|
|
i.value = i.value + "/";
|
|
}
|
|
} else {
|
|
if (direction == "up") {
|
|
if (i.value.length == 0) {
|
|
i.value = ""
|
|
} else {
|
|
i.value = i.value.substring(0,i.value.length-1)
|
|
}
|
|
}
|
|
}
|
|
i.focus()
|
|
}
|
|
} else {
|
|
if (direction == "down") {
|
|
return checkDate(i)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
function checkDate(THISDATE) {
|
|
if(THISDATE.value=="")
|
|
return true;
|
|
|
|
var err=0
|
|
a=THISDATE.value;
|
|
|
|
if (a.length != 10 && a.length != 6 && a.length != 8) {err=1;}
|
|
//ho invertito b con di per otternere il formato dd/mm/yyyy
|
|
d = a.substring(0, 2)// month-->day
|
|
c = a.substring(2, 3)// '/'
|
|
b = a.substring(3, 5)// day-->month
|
|
e = a.substring(5, 6)// '/'
|
|
if (a.length == 10)
|
|
f = a.substring(6, 10)// year
|
|
else
|
|
if (a.length == 8) {
|
|
f = a.substring(6, 8)// year
|
|
f=f+2000;
|
|
}
|
|
else
|
|
{
|
|
//dovrei mettere l'anno corrente
|
|
var today = new Date();
|
|
f = today.getFullYear();
|
|
|
|
}
|
|
|
|
if (b<1 || b>12) err = 1
|
|
if (d<1 || d>31) err = 1
|
|
if (a.length == 10 && f<1800) err = 1
|
|
if (b==4 || b==6 || b==9 || b==11){
|
|
if (d>=31) err=1
|
|
}
|
|
if (b==2){
|
|
var g=parseInt(f/4)
|
|
if (isNaN(g)) {
|
|
err=1
|
|
}
|
|
if (d>29) err=1
|
|
if (d==29 && ((f/4)!=parseInt(f/4))) err=1
|
|
}
|
|
if (err==1) {
|
|
alert(THISDATE.value + " non e' una data valida! ");
|
|
THISDATE.value = ""
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function printDate(THISDATE, campo) {
|
|
|
|
var err=0
|
|
a=THISDATE.value
|
|
if (a.length != 10) err=1
|
|
//ho invertito b con di per otternere il formato dd/mm/yyyy
|
|
day = a.substring(0, 2)// month-->day
|
|
c = a.substring(2, 3)// '/'
|
|
month = a.substring(3, 5)// day-->month
|
|
e = a.substring(5, 6)// '/'
|
|
if (a.length == 10)
|
|
f = a.substring(6, 10)// year
|
|
else
|
|
if (a.length == 8) {
|
|
f = a.substring(6, 8)// year
|
|
f=f+2000;
|
|
}
|
|
else
|
|
{
|
|
//dovrei mettere l'anno corrente
|
|
var today = new Date();
|
|
f = today.getFullYear();
|
|
|
|
}
|
|
|
|
var now = new Date(year,month-1,day,0,0,0,0);
|
|
|
|
// Array list of days.
|
|
var days = new Array('Domenica','lunedi','Martedi','Mercoledi','Giovedi','Venerdi','Sabato');
|
|
// Calculate the number of the current day in the week.
|
|
//var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
|
|
|
|
giorno = days[now.getDay()] ;
|
|
|
|
var campoData=document.getElementById(campo);
|
|
|
|
campoData.value=giorno;
|
|
|
|
}
|
|
|
|
|
|
function formatTime(i, delKey, direction) {
|
|
//formatHuor(this, window.event.keyCode,'down')
|
|
if (i.value.length < 10) {
|
|
if (delKey!=9) { //tab
|
|
if(delKey!=8 && delKey!=46 && delKey!=16 && !(delKey>36 && delKey<41)){ //if the delete, backspace, shift, are not the keys that caused the keyup event.
|
|
var fieldLen = i.value.length
|
|
if ((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <=105)) {
|
|
if (fieldLen == 2 ) {
|
|
i.value = i.value + ":";
|
|
}
|
|
} else {
|
|
if (direction == "up" && delKey!=13 ) {
|
|
if (i.value.length == 0) {
|
|
i.value = ""
|
|
} else {
|
|
i.value = i.value.substring(0,i.value.length-1)
|
|
}
|
|
}
|
|
}
|
|
i.focus()
|
|
}
|
|
} else {
|
|
if (direction == "down") {
|
|
//CheckDate(i)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Funzione per formattare il time input in uscita dalla input
|
|
*/
|
|
function parseTime(i)
|
|
{
|
|
var val = i.value,
|
|
field = $("#" + i.id),
|
|
valori = val.split(":"),
|
|
hh = "",
|
|
mm = "";
|
|
|
|
|
|
if ($(field).attr("readonly") == undefined)
|
|
{
|
|
// controllo che il valore sia lungo 5 caratteri
|
|
if (val.trim().length > 5)
|
|
{
|
|
alert("Inserire un orario valido!");
|
|
$(field).focus();
|
|
}
|
|
|
|
// carico i valori di ore e minuti
|
|
if (valori.length > 0)
|
|
{
|
|
hh = valori[0];
|
|
}
|
|
if (valori.length > 1)
|
|
{
|
|
mm = valori[1];
|
|
}
|
|
|
|
|
|
// controllo formale sui valori inseriti
|
|
if (hh.trim() != "" && hh > 23) {
|
|
hh = "00";
|
|
}
|
|
if (mm.trim() != "" && mm > 59) {
|
|
mm = "00";
|
|
}
|
|
|
|
|
|
// formattazione valori per visualizzazione
|
|
if (hh.trim() == "") {
|
|
hh = "00";
|
|
}
|
|
if (hh.trim() != "" && hh.trim().length == 1)
|
|
{
|
|
hh = "0" + hh;
|
|
}
|
|
|
|
if (mm.trim() == "")
|
|
{
|
|
mm = "00";
|
|
}
|
|
if (mm.trim().length == 1)
|
|
{
|
|
mm += "0";
|
|
}
|
|
|
|
$(field).val(hh + ":" + mm);
|
|
|
|
//console.log(hh + " " + mm);
|
|
}
|
|
|
|
/*
|
|
if (val.length > 0 && val.length < 5)
|
|
{
|
|
if (val.length < 3)
|
|
{
|
|
val = val + ":00";
|
|
}
|
|
else
|
|
{
|
|
val = val + "00";
|
|
}
|
|
|
|
$(field).val(val);
|
|
}
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
//formatHuor(this, window.event.keyCode,'down')
|
|
if (i.value.length < 10) {
|
|
if (delKey!=9) { //tab
|
|
if(delKey!=8 && delKey!=46 && delKey!=16 && !(delKey>36 && delKey<41)){ //if the delete, backspace, shift, are not the keys that caused the keyup event.
|
|
var fieldLen = i.value.length
|
|
if ((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <=105)) {
|
|
if (fieldLen == 2 ) {
|
|
i.value = i.value + ":";
|
|
}
|
|
} else {
|
|
if (direction == "up" && delKey!=13 ) {
|
|
if (i.value.length == 0) {
|
|
i.value = ""
|
|
} else {
|
|
i.value = i.value.substring(0,i.value.length-1)
|
|
}
|
|
}
|
|
}
|
|
i.focus()
|
|
}
|
|
} else {
|
|
if (direction == "down") {
|
|
//CheckDate(i)
|
|
}
|
|
}
|
|
}
|
|
|
|
*/
|
|
}
|