Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 17
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093

    validazione data formato gg/mm/YYYY

    Ciao a tutti,

    lo so che se ne sdara paralato moltissimo ma non so come modificare e usare le reg exp per js...

    Io ho un campo form dove l' utente deve inserire solo la data in formato mm/gg/yyyy e solo con gli /
    verificando che non metta 99/99/9999

    possibile aiuto?

    grazie

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    ho provato a fare questo , non sapendo se corretto o meno ma non so come usarla:

    var i=new RegExp("^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$");

    grazie

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2001
    Messaggi
    21,188
    Io di solito uso questa sintassi:

    var str = document.NOMEFORM.NOMECAMPO.value; // o equivalente
    var RE = /.../
    if (!str.match(RE)) {
    alert("data errata");
    return false;
    }
    ...


    Dove RE io la metterei:
    /^([012]\d)|3[01])\/(0\d)|(1[012])\/(19)|(20)\d{2}$/

    Nota che in questo caso la stringa RE viene limitata da / , non da " , ma forse e` megli oscrivere la RE direttamente nel parametro del metodo .match().

    Sono possibili altre sintassi, tipo l'uso di oggetti RegExp (cui puoi applicare il metodo .test() ), oppure usando altri metodi di stringa.
    Nuova politica di maggiore severita` sui titoli delle discussioni: (ri)leggete il regolamento
    No domande tecniche in messaggi privati

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    provo e ti dico
    grazie

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    hi orivato ma non funziona e non capisco l' erore


    <script>
    function pro(){
    var str = document.pro.uno.value;
    var RE = /^([012]\d)|3[01])\/(0\d)|(1[012])\/(19)|(20)\d{2}$/;
    if (!str.match(RE)) {
    alert("data errata");
    return false;
    }
    }
    </script>

    <form name="pro">
    <input type="text" name="uno" onclick="pro()">
    </form>

  6. #6
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    di solito uso questo script che ho fatto io :

    1) bisogna includere il file di controllo format data




    2) Nel controllo del Submit del form si esegue questo controllo :

    if (document.all.dta_iscrizione.value == ""){
    messaggio = "Data iscrizione
    obbligatoria."
    NewWindow("../../../Include/PopUpMessaggi.asp?msg="+ messaggio,"dataitem",'110','230');
    return false;
    }else{
    ctrlDta = formatoData(document.all.dta_iscrizione.value)
    if (ctrlDta != "OK"){
    messaggio = "Formato data iscrizione
    errato."
    NewWindow("../../../Include/PopUpMessaggi.asp?msg="+messaggio,"dataitem",'110' ,'230');
    return false;
    }
    }


    3) questo è il codice dell'include formatData.js :



    // ****************************
    // FUNZIONE PER CONTROLLO DATE
    // ****************************
    function formatoData(parametro){
    dtaIni = ""
    dtaIni = parametro

    giorno = dtaIni.substring(0,2)
    if (giorno.substring(0,1) == "0"){
    giorno = giorno.substring(1,2)
    }

    mese = dtaIni.substring(3,5)
    if (mese.substring(0,1) == "0"){
    mese = mese.substring(1,2)
    }

    anno = dtaIni.substring(6,10)


    if (dtaIni.length != 10){ // CONTROLLO LUNGHEZZA ESATTA DATA
    return "ERRORE LUNGHEZZA"
    }
    if (dtaIni.substr(0,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 1"
    }
    if (dtaIni.substr(1,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 2"
    }
    if (dtaIni.substr(3,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 3"
    }
    if (dtaIni.substr(4,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 4"
    }
    if (dtaIni.substr(6,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 5"
    }
    if (dtaIni.substr(7,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 6"
    }
    if (dtaIni.substr(8,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 7"
    }
    if (dtaIni.substr(9,1) == "/"){ // CONTROLLO BARRA IN POSIZOINE ERRATA
    return "ERRORE BARRA 8"
    }
    if (dtaIni.substr(2,1) != "/"){ // CONTROLLO BARRA DOPO GIORNO
    return "ERRORE BARRA 9"
    }
    if (dtaIni.substr(5,1) != "/"){ // CONTROLLO BARRA DOPO MESE
    return "ERRORE BARRA 10"
    }
    if (parseInt(mese) < 1){ // CONTROLLO LIMITE MINIMO MESE
    return "ERRORE MESE MIN"
    }
    if (parseInt(mese) > 12){ // CONTROLLO LIMITE MASSIMO MESE
    return "ERRORE MESE MAX"
    }
    if (parseInt(dtaIni.substr(6,4)) < 1900){ // CONTROLLO LIMITE MINIMO ANNO
    return "ERRORE ANNO MIN"
    }
    if (parseInt(dtaIni.substr(6,4)) > 2100){ // CONTROLLO LIMITE MASSIMO ANNO
    return "ERRORE ANNO MAX"
    }
    if (parseInt(giorno) < 1){ // CONTROLLO LIMITE MINIMO GIORNO
    return "ERRORE GIORNO MIN"
    }
    if (parseInt(mese) == 1){ // CONTROLLO LIMITE GIORNI GENNAIO
    if (parseInt(giorno) > 31){
    return "ERRORE GENNAIO MAX"
    }
    }
    if (parseInt(mese) == 2){ // CONTROLLO LIMITE GIORNI FEBBRAIO

    // CALCOLO BISESTILE ( divisibile per 4 e non per 100, o , divisibile per 400 )
    yr = parseInt(anno)
    rem = yr % 4;
    if(rem == 0) {
    div4 = true;
    } else {
    div4 = false;
    }
    rem = yr % 100;
    if(rem == 0) {
    div100 = true;
    } else {
    div100 = false;
    }
    rem = yr % 400;
    if(rem == 0) {
    div400 = true;
    } else {
    div400 = false;
    }
    if ( (div4 == true && div100 == false) || (div400 == true) ){
    if (parseInt(giorno) > 29){
    return "ERRORE ANNO BISESTILE"
    }
    } else {
    if (parseInt(giorno) > 28){
    return "ERRORE ANNO NON BISESTILE"
    }
    }
    }
    if (parseInt(mese) == 3){ // CONTROLLO LIMITE GIORNI MARZO
    if (parseInt(giorno) > 31){
    return "ERRORE MARZO MAX"
    }
    }
    if (parseInt(mese) == 4){ // CONTROLLO LIMITE GIORNI APRILE
    if (parseInt(giorno) > 30){
    return "ERRORE APRILE MAX"
    }
    }
    if (parseInt(mese) == 5){ // CONTROLLO LIMITE GIORNI MAGGIO
    if (parseInt(giorno) > 31){
    return "ERRORE MAGGIO MAX"
    }
    }
    if (parseInt(mese) == 6){ // CONTROLLO LIMITE GIORNI GIUGNO
    if (parseInt(giorno) > 30){
    return "ERRORE GIUGNO MAX"
    }
    }
    if (parseInt(mese) == 7){ // CONTROLLO LIMITE GIORNI LUGLIO
    if (parseInt(giorno) > 31){
    return "ERRORE LUGLIO MAX"
    }
    }
    if (parseInt(mese) == 8){ // CONTROLLO LIMITE GIORNI AGOSTO
    if (parseInt(giorno) > 31){
    return "ERRORE AGOSTO MAX"
    }
    }
    if (parseInt(mese) == 9){ // CONTROLLO LIMITE GIORNI SETTEMBRE
    if (parseInt(giorno) > 30){
    return "ERRORE SETTEMBRE MAX"
    }
    }
    if (parseInt(mese) == 10){ // CONTROLLO LIMITE GIORNI OTTOBRE
    if (parseInt(giorno) > 31){
    return "ERRORE OTTOBRE MAX"
    }
    }
    if (parseInt(mese) == 11){ // CONTROLLO LIMITE GIORNI NOVEMBRE
    if (parseInt(giorno) > 30){
    return "ERRORE NOVEMBRE MAX"
    }
    }
    if (parseInt(mese) == 12){ // CONTROLLO LIMITE GIORNI DICEMBRE
    if (parseInt(giorno) > 31){
    return "ERRORE DICEMBRE MAX"
    }
    }

    return "OK"
    }

    ciao !!!
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  7. #7
    Utente di HTML.it
    Registrato dal
    Sep 2001
    Messaggi
    21,188
    Originariamente inviato da roboro
    hi orivato ma non funziona e non capisco l' erore
    ...
    var RE = /^([012]\d)|3[01])\/(0\d)|(1[012])\/(19)|(20)\d{2}$/;
    ....
    Togli il puntoevirgola alla fine della stringa: se lo porta dietro dentro il codice.
    Nuova politica di maggiore severita` sui titoli delle discussioni: (ri)leggete il regolamento
    No domande tecniche in messaggi privati

  8. #8
    Utente di HTML.it L'avatar di willybit
    Registrato dal
    May 2001
    Messaggi
    4,367
    Ciao a tutti,

    visto che ci siamo metto la funzioncina che uso di solito io
    codice:
    //controllo data valida in formato gg/mm/aaaaa
    function dataValida(txt){
    	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
    	if(re.test(txt)){
    		var adata = txt.split('/');
    		var gg = parseInt(adata[0],10);
    		var mm = parseInt(adata[1],10);
    		var aaaa = parseInt(adata[2],10);
    		var xdata = new Date(aaaa,mm-1,gg)
    		if((xdata.getFullYear()==aaaa) && (xdata.getMonth()==mm-1) && (xdata.getDate()==gg))
    			return xdata
    		else return false
    	}else return false
    }

  9. #9
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    sicuramente più pulita della mia !!!!!
    mi spieghi questa riga, però :

    codice:
     
    var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
    	if(re.test(txt)){
    non la capisco
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  10. #10
    Utente di HTML.it
    Registrato dal
    Jan 2003
    Messaggi
    1,093
    grazie mille a tutti...le provo...

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.