IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

force le format d 'un champ


Sujet :

JavaScript

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2002
    Messages
    193
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2002
    Messages : 193
    Points : 66
    Points
    66
    Par défaut force le format d 'un champ
    bonjour a tous,

    j aimerais pouvoir force le format d un champ dans un formulaire ...
    le champ devrais etre de type date jj/mm/aaaa

    je suis vraiment null en javascript, et j en ai absolument besoin ....
    qui pourrais m aider ?

    je vous en remercie d avance

  2. #2
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 669
    Points
    66 669
    Billets dans le blog
    1
    Par défaut
    oulà le format date ....
    moi je passe toujours par des calendriers de saisie c'est nettement plus sur que des controles avec des regExp ...
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2002
    Messages
    193
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2002
    Messages : 193
    Points : 66
    Points
    66
    Par défaut
    ben moi je suis ouvert a toute proposition si tu peux m expliquer comment cela marche ? ou bien me donner un liens ou l on explique cela ???

  4. #4
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 669
    Points
    66 669
    Billets dans le blog
    1
    Par défaut
    le but est d'afficher un calendrier qui permette la saisie... je dois avoir un truc du genre en stock ...

    mhh c'est un peu long mais bon:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    <html>
     
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Calendrier SpaceFrog</title>
    <script type='text/javascript'>
    function show_calendar(str_target, str_datetime) {
       var arr_months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin",
          "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
       var week_days = ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"];
       var n_weekstart = 1; // day week starts from (normally 0 or 1)
     
    // Si la date est invalide : Initialisation avec la date du jour
    var test_date = /^(\d+)\/(\d+)\/(\d+)$/;
    if (!test_date.exec(str_datetime))
    {
    var d = new Date();
     
    var str_datetime = d.getDate()+ "/" +(d.getMonth() + 1) + "/"+  d.getYear();
     
    }
     
       var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));
     
       var dt_prev_month = new Date(dt_datetime);
       dt_prev_month.setMonth(dt_datetime.getMonth()-1);
       var dt_next_month = new Date(dt_datetime);
       dt_next_month.setMonth(dt_datetime.getMonth()+1);
     
       var dt_prev_year = new Date(dt_datetime);
       dt_prev_year.setFullYear(dt_datetime.getFullYear()-1);
       var dt_next_year = new Date(dt_datetime);
       dt_next_year.setFullYear(dt_datetime.getFullYear()+1);
     
       var dt_firstday = new Date(dt_datetime);
       dt_firstday.setDate(1);
       dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
       var dt_lastday = new Date(dt_next_month);
       dt_lastday.setDate(0);
       var title_calendar=(str_target=='dd')?'DATE D\'ARRIVEE':'DATE DE DEPART';
     
       // html generation (feel free to tune it for your particular application)
       // print calendar header
       var StringYear=dt_datetime.getFullYear().toString().substring(dt_datetime.getFullYear().toString().length-2,dt_datetime.getFullYear().toString().length)
       var str_buffer = new String (
          "<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
          "<tr>\n<td bgcolor=\"#4682B4\">"+
          "<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
     
    	//ENTETE TABLEAU
    	"<tr>\n   <td bgcolor=\"blue\" style='text-align:center;color:white;font-family:verdana;font-weight:bold;font-size:11px;' colspan=\"6\">"+title_calendar+"<\/td>\n"+
    	"<td bgcolor=\"blue\" style=\"text-align:center;color:navy;font-family:verdana;font-weight:bold;font-size:10px;background-color:silver;border:outset 2px white;cursor:pointer;\" onclick=\"document.getElementById('DivCalendar').style.display='none';\">X<\/td>\n "+
    	"<\/tr>\n"+
     
     
     
    // Ligne Année précédente / suivante
          "<tr>\n   <td bgcolor=\"blue\"><a href=\"javascript:show_calendar('"+
          str_target+"', '"+ dt2dtstr(dt_prev_year)+"');\">"+
          "<img src=\"images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
          " alt=\"Année précédente\"></a></td>\n"+
          "   <td align=\"center\" bgcolor=\"blue\" colspan=\"5\">"+
          "<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
          +" 20"+StringYear+"</font></td>\n"+
          "   <td bgcolor=\"blue\" align=\"right\"><a href=\"javascript:show_calendar('"
          +str_target+"', '"+dt2dtstr(dt_next_year)+"');\">"+
          "<img src=\"images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
     
          " alt=\"Année suivante\"></a></td>\n</tr>\n"+
     
    // Ligne Mois précédent / suivant
          "<tr>\n   <td bgcolor=\"#4682B4\"><a href=\"javascript:show_calendar('"+
          str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\">"+
          "<img src=\"images/prev.gif\" width=\"16\" height=\"16\" border=\"0\""+
          " alt=\"Mois précédent\"></a></td>\n"+
          "   <td align=\"center\" bgcolor=\"#4682B4\" colspan=\"5\">"+
          "<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
          +arr_months[dt_datetime.getMonth()]+" 20"+StringYear+"</font></td>\n"+
          "   <td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:show_calendar('"
          +str_target+"', '"+dt2dtstr(dt_next_month)+"');\">"+
          "<img src=\"images/next.gif\" width=\"16\" height=\"16\" border=\"0\""+
          " alt=\"Mois suivant\"></a></td>\n</tr>\n"
     
       ); //end newstring
     
       var dt_current_day = new Date(dt_firstday);
     
       // print weekdays titles
       str_buffer += "<tr>\n";
       for (var n=0; n<7; n++)
          str_buffer += "   <td bgcolor=\"#87CEFA\">"+
          "<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
          week_days[(n_weekstart+n)%7]+"</font></td>\n";
       // print calendar table
       str_buffer += "</tr>\n";
       while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
          dt_current_day.getMonth() == dt_firstday.getMonth()) {
          // print row header
          str_buffer += "<tr>\n";
          for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
                if (dt_current_day.getDate() == dt_datetime.getDate() &&
                   dt_current_day.getMonth() == dt_datetime.getMonth())
                   // print current date
                   str_buffer += "   <td bgcolor=\"#FFB6C1\" align=\"right\">";
                else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
                   // weekend days
                   str_buffer += "   <td bgcolor=\"#DBEAF5\" align=\"right\">";
                else
                   // print working days of current month
                   str_buffer += "   <td bgcolor=\"white\" align=\"right\">";
     
                if (dt_current_day.getMonth() == dt_datetime.getMonth())
                   // print days of current month
                   str_buffer += "<a style=\"cursor:pointer;\" onclick=\"document.getElementById('"+str_target+"').value='"+dt2dtstr(dt_current_day)+"'; document.getElementById('DivCalendar').style.display='none';\">"+
     
                   "<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
                else
                   // print days of other months
     
     
                   str_buffer += "<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
                str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
     
                dt_current_day.setDate(dt_current_day.getDate()+1);
          }
          // print row footer
          str_buffer += "</tr>\n";
       }
       // print calendar footer
       str_buffer +=  "</tr>\n</td>\n</table>\n" ;
     
     
     
       fenCalendrier=document.getElementById('DivCalendar');
       fenCalendrier.innerHTML=str_buffer;
       fenCalendrier.style.top=document.getElementById(str_target).offsetTop+"px";
       fenCalendrier.style.left=Number(document.getElementById(str_target).offsetLeft)+Number(document.getElementById(str_target).offsetWidth)+"px";
       fenCalendrier.style.display="block"
     
    }
    // datetime parsing and formatting routimes. modify them if you wish other datetime format
    function str2dt (str_datetime) {
       var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
       if (!re_date.exec(str_datetime))
          return alert("Invalid Datetime format: "+ str_datetime);
       return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
    }
     
     
     
    /***********************************
    * Formatage de date pour affichage *
    ***********************************/
     
    function dt2dtstr (dt_datetime) {
       var FormatedDate=""
     
       FormatedDate+=(dt_datetime.getDate().toString().length==1)?"0"+dt_datetime.getDate().toString():dt_datetime.getDate().toString();
       FormatedDate+="/";
     	FormatedDate+=((dt_datetime.getMonth()+1).toString().length==1)?"0"+(dt_datetime.getMonth()+1).toString():(dt_datetime.getMonth()+1).toString();
    	FormatedDate+="/";
    	FormatedDate+="20"+dt_datetime.getFullYear().toString().substring(dt_datetime.getFullYear().toString().length-2,dt_datetime.getFullYear()).toString()
    	return FormatedDate;
    }
     
    function dt2tmstr (dt_datetime) {
       return (new String (
             dt_datetime.getHours()+":"+dt_datetime.getMinutes()+":"+dt_datetime.getSeconds()));
    } 
     
     
     
    /*************************
    * comparaison des dates  *
    *************************/
    function comparedate(){
     
    var maintenant=new Date()
    maintenant=dt2dtstr(maintenant).split('\/').reverse().join('')
     
     
     
    if(document.getElementById('da').value.length<1){
    															alert("Entrez une date d\'arrivee");
    															return false;
    															}
     
    var date_arrivee=document.getElementById('da').value.split('\/').reverse().join('');
    if(date_arrivee<maintenant){
                                 alert('vous ne pourrez pas arriver avant aujourd\'hui');
                                 return false;
                                 }
     
    if(document.getElementById('da').value.length<1){
                                                     alert("Entrez une date de départ"); 
                                                     return false;
                                                     }
     
    var date_depart=document.getElementById('dd').value.split('\/').reverse().join('');
    if(date_arrivee>date_depart){
                                 alert("Vous devez arriver avant de repartir!"); 
                                 return false;
                                 }
     
     
    else {		
    		return true;}
     
    }
    ('votre nom')
    </script>
    </head>
     
    <body >
    <div id="DivCalendar" style="z-index:2; display:none; width:200px;height:235px; overflow:hidden;position:absolute;Top:1px; left:1px;" ></div>
    <form name="calendar" action="javascript:alert('ok')" onsubmit="return comparedate()" method="post">
    <input type='text' name='da' id='da' onfocus="show_calendar('da','jj,mm,aaaa')" style='text-align:right' readonly/><br/>
    <input type='text' name='dd' id='dd' onfocus="show_calendar('dd')" readonly style='text-align:right' />
    <input type='submit' value='envoyer'/>
    </form>
    </body>
     
    </html>
    a perfectionner sans doute et à adapter ...
    sinon y'en a plein d'autres sur le net
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  5. #5
    Membre éclairé
    Inscrit en
    Janvier 2004
    Messages
    633
    Détails du profil
    Informations forums :
    Inscription : Janvier 2004
    Messages : 633
    Points : 721
    Points
    721

  6. #6
    Rédacteur/Modérateur

    Avatar de SpaceFrog
    Homme Profil pro
    Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Inscrit en
    Mars 2002
    Messages
    39 640
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Bidouilleur SharePoint
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2002
    Messages : 39 640
    Points : 66 669
    Points
    66 669
    Billets dans le blog
    1
    Par défaut
    lol comme quoi je n'ai pas de mémoire mais un minimum de constance dans mes réponses ...
    Ma page Developpez - Mon Blog Developpez
    Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
    Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
    Votre post est résolu ? Alors n'oubliez pas le Tag

    Venez sur le Chat de Développez !

  7. #7
    Membre éclairé
    Inscrit en
    Janvier 2004
    Messages
    633
    Détails du profil
    Informations forums :
    Inscription : Janvier 2004
    Messages : 633
    Points : 721
    Points
    721
    Par défaut
    J'ai pas trop de mérite je suis tombé dessus hier et cela m'a bien aidé

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2002
    Messages
    193
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juillet 2002
    Messages : 193
    Points : 66
    Points
    66
    Par défaut
    merci a tous

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Modifier le format d'un champ depuis VBA
    Par Psikorene dans le forum VBA Access
    Réponses: 6
    Dernier message: 12/03/2007, 14h02
  2. Formulaire Format d'un champs adresse TCP/IP
    Par sperchey dans le forum IHM
    Réponses: 8
    Dernier message: 15/02/2007, 13h30
  3. [ACCESS2002] [format d'un champs] - DATE
    Par louroulou dans le forum IHM
    Réponses: 6
    Dernier message: 13/06/2006, 16h40
  4. Changer le format d'un champ d'une table
    Par Cablan dans le forum Access
    Réponses: 2
    Dernier message: 14/12/2005, 13h07
  5. [VB.NET] vérifier le format d'un champ dans un datatable
    Par HULK dans le forum Windows Forms
    Réponses: 5
    Dernier message: 27/10/2005, 16h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo