Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Validation de formulaire sans JavaScript</h1>
<p>Pour ne pas écrire une ligne de JavaScript afin de valider vos formulaires, il vous faudra 3 scripts.</p>

<ul>
  <li><a href="https://jquery.com/">jQuery</a> (//code.jquery.com/jquery-2.1.4.min.js)</li>
  <li><a href="https://jqueryvalidation.org/">jQuery Validation Plugin</a> (//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js)</li>
  <li><a href="https://www.nuget.org/packages/jQuery.Validation.Unobtrusive/">jQuery Validation Unobstrusive Plugin</a> (//ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.min.js)</li>
</ul>

<h2>Formulaire standard</h2>
<form action="">
  <fieldset>
    <legend>Champs simple</legend>
    
    <label for="pseudo">Pseudo</label>
    <input 
           type="text" 
           name="pseudo"     
           placeholder="Haeresis"
           data-rule-required="true"
           data-msg-required="Le champ Pseudo est requis."
    />
    <span data-valmsg-for="pseudo" data-valmsg-replace="true"></span>
  </fieldset>
  
  <fieldset>
    <legend>Champs email</legend>
    <label for="email">Email</label>
    <input 
           type="text" 
           name="email"
           placeholder="bruno.lesieur@gmail.com"
           data-rule-required="true"
           data-msg-required="Le champ Email est requis."
           data-rule-email="true"
           data-msg-email="Le champ Email n'a pas le bon format."
    />
    <span data-valmsg-for="email" data-valmsg-replace="true"></span>
  </fieldset>
  
  <fieldset>
    <legend>Champs mot de passe</legend>
    <div>
      <label for="password">Mot de passe</label>
      <input 
             type="password" 
             name="password"
             placeholder="Mot de passe"
             data-rule-required="true"
             data-msg-required="Le champ Password est requis."
             data-val="true"
             data-val-regex-pattern="^(?=.*[A-Za-z])(?=.*\d).{6,}$"
             data-val-regex="Le champ Password doit faire au minimum 6 caractères, et inclure une lettre et un nombre."
      />
      <span data-valmsg-for="password" data-valmsg-replace="true"></span>
    </div>
    <div>
      <label for="confirm">Confirmation</label>
      <input 
           type="password" 
           name="confirm"
           placeholder="Confirmer le mot de passe"
           data-val="true"
           data-val-equalto="Le mot de passe de confirmation ne correspond pas au mot de passe."
           data-val-equalto-other="password"
      />
      <span data-valmsg-for="confirm" data-valmsg-replace="true"></span>
    </div>
  </fieldset>

  <fieldset>
    <legend>Champs de choix</legend>

    <div>
      <label for="gender">Sexe</label>
      <input 
           type="radio" 
           name="gender" 
           id="male" 
           value="choice-1"
           data-rule-required="true"
           data-msg-required="Le choix Sexe est requis."
      />
      <label for="male">Homme</label>

      <input type="radio" name="gender" id="female" value="choice-2" />
      <label for="female">Femme</label>
    
      <span data-valmsg-for="gender" data-valmsg-replace="true"></span>
    </div>
  </fieldset>
  
  <fieldset>
    <legend>Champs de sélection</legend>
    <div>
      <label for="relationship">Situation</label>
      <select 
              name="relationship"
              data-rule-required="true"
              data-msg-required="La sélection Situation est requise."
        >
        <option value="">Sélectionner</option>
        <option value="avenue">Célibataire</option>
        <option value="chemin">En couple</option>
        <option value="rue">Marié</option>
      </select>
      <span data-valmsg-for="relationship" data-valmsg-replace="true"></span>
    </div>
  </fieldset>

  <fieldset>
    <legend>Champs texte</legend>
    <label for="description">Description</label>
    <div>
      <textarea 
              cols="40"
              rows="8"
              name="description"
              data-rule-required="true"
              data-msg-required="Le texte Description est requis."
              data-rule-minlength="10"
              data-msg-minlength="Le texte Description doit faire au minimum 10 caractères."
              data-rule-maxlength="100"
              data-msg-maxlength="Le texte Description doit faire au maximum 100 caractères."
      ></textarea>
    </div>
    <span data-valmsg-for="description" data-valmsg-replace="true"></span>
  </fieldset>

  <fieldset>
    <legend>Champs case</legend>
    <div>
      <input 
             type="checkbox" 
             name="terms"
             id="terms"
             data-rule-required="true"
             data-msg-required="La case Accepter les termes et conditions est requise."
      />
      <label for="terms">Accepter les termes et conditions</label>
      <span data-valmsg-for="terms" data-valmsg-replace="true"></span>
    </div>
  </fieldset>

  <fieldset>
    <legend>Champs date</legend>
    
    <label for="birthdate">Anniversaire</label>
    <input 
           type="text" 
           name="birthdate"     
           placeholder="dd/mm/yyyy"
           data-rule-required="true"
           data-msg-required="Le champ Anniversaire est requis."
           data-val="true"
           data-val-regex="Une date valide est au format dd/mm/yyyy" 
           data-val-regex-pattern="^([0-9]{2})/([0-9]{2})/([0-9]{4})$"
           data-val-regex-flags="g"
    />
    <span data-valmsg-for="birthdate" data-valmsg-replace="true"></span>
  </fieldset>

  <fieldset>
    <legend>Champs nombre</legend>
    
    <label for="age">Age</label>
    <input 
           type="text" 
           name="age"     
           placeholder="Nombre d'année"
           data-rule-required="true"
           data-msg-required="Le champ Age est requis."
           data-val="true"
           data-val-regex="Entrez un nombre entre 1 et 2 chiffres" 
           data-val-regex-pattern="\d{1,2}"
           data-val-range="Un age authorisé est de 7 à 77 ans" 
            data-val-range-max="77" data-val-range-min="7"
    />
    <span data-valmsg-for="age" data-valmsg-replace="true"></span>
  </fieldset>

  <fieldset>
    <legend>Champs url</legend>
    <label for="website">Site</label>
    <input 
           type="text" 
           name="website"
           placeholder="http://www.exemple.com/"
           data-rule-required="true"
           data-msg-required="Le champ Site est requis."
           data-rule-url="true"
           data-msg-url="Le champ Site n'a pas le bon format."
    />
    <span data-valmsg-for="website" data-valmsg-replace="true"></span>
  </fieldset>

  <fieldset>
    <legend>Champs caché</legend>
    <div>
      <label for="hidden">Caché avec type hidden</label>
      <input 
             type="hidden" 
             name="hidden"
             data-rule-required="true"
             data-msg-required="Le champs Caché avec type hidden est requis."
      />
      <span data-valmsg-for="hidden" data-valmsg-replace="true"></span>
    </div>
    <div>
      <label for="hidden-css">Caché en CSS</label>
      <input 
             class="hidden"
             type="text" 
             name="hidden-css"
             data-rule-required="true"
             data-msg-required="Le champs Caché en CSS ! est requis."
      />
      <span data-valmsg-for="hidden-css" data-valmsg-replace="true"></span>
    </div>
  </fieldset>

  <fieldset>
    <legend>Champs asynchrone</legend>
    <div class="asynchone-field">
      Un champ va arriver de manière asynchrone.
    </div>
  </fieldset> 

  <hr />

  <div class="validation-summary-valid" data-valmsg-summary="true">
   <ul>
     <li style="display:none"></li>
   </ul>
  </div>

  <div>
    <input id="visible" type="submit" value="Valider les champs visible" />
  </div>

  <hr />

  <div>
    <input id="all" type="submit" value="Valider tous les champs" />
  </div>

  <hr />

  <div>
    <input id="ignore" type="submit" value="Valider tous les champs, sauf ceux inclu dans « .hidden »" />
  </div>
</form>

<hr />

<h2>Formulaire asynchrone</h2>
<div class="asynchone-form">
  Un formulaire va arriver de manière asynchrone.
</div>
              
            
!

CSS

              
                /* Les messages d'erreur en rouge */
.validation-summary-errors,
.field-validation-error {
  color: #f00;
}
.input-validation-error {
  border: 1px solid #f00;
}

/* Cacher des champs */
.hidden {
  display: none;
}

/* Champs désactivé */
[disabled] {
  opacity: 0.5;
}





/* Style pour embeded page */
html {
  font-size: 62.5%;
}
body {
  margin: 0 0 2px;
  padding: 8px;
  border: 1px dashed #ccc;
  font-family: Lato, "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  font-size: 1.4rem;
  line-height: 21px;
  color: #777;
  font-weight: 300;
  padding: 8px;
}
h1 {
  color: #222;
  margin: 32px 0 0;
  font-weight: 400;
  line-height: 1;
}
              
            
!

JS

              
                /*****************************************\
  Valider tous les champs visible en 
  n'ajoutant rien ou 
  en ajoutant la ligne JavaScript
  « $.validator.setDefaults({ ignore: ":hidden" }); »
\*****************************************/

$("#visible").click(function (event) {
  event.preventDefault();
  $("#ignore").attr("disabled", true).after($("<span>").text(" Recharger la page pour tester les autres boutons (RERUN bouton)."));
  $("#all").attr("disabled", true).after($("<span>").text(" Rechargé la page pour tester les autres boutons (RERUN bouton)."));
  $(this).parents('form').validate().settings.ignore = ":hidden";
  $(this).parents('form').submit();
});



/*****************************************\
  Valider les champs invisible 
  en ajoutant la ligne JavaScript
  « $.validator.setDefaults({ ignore: [] }); »
\*****************************************/

$("#all").click(function (event) {
  event.preventDefault();
  $("#visible").attr("disabled", true).after($("<span>").text(" Recharger la page pour tester les autres boutons (RERUN bouton)."));
  $("#ignore").attr("disabled", true).after($("<span>").text(" Recharger la page pour tester les autres boutons (RERUN bouton)."));
  $(this).parents('form').validate().settings.ignore = [];
  $(this).parents('form').submit();
});



/*****************************************\
  Valider tous les champs sauf ceux « .hidden » 
  en ajoutant la ligne JavaScript
  « $.validator.setDefaults({ ignore: ".hidden" }); »
\*****************************************/

$("#ignore").click(function (event) {
  event.preventDefault();
  $("#visible").attr("disabled", true).after($("<span>").text(" Recharger la page pour tester les autres boutons (RERUN bouton)."));
  $("#all").attr("disabled", true).after($("<span>").text(" Rechargé la page pour tester les autres boutons (RERUN bouton)."));
  $(this).parents('form').validate().settings.ignore = ".hidden";
  $(this).parents('form').submit();
});



/*****************************************\
  Des nouveaux champs peuvent être ajouté à
  la volée.
\*****************************************/

$(function () {
  setTimeout(function () {
    $(".asynchone-field").html('<label for="async-field">Asynchrone</label> <input type="text" name="async-field" data-rule-required="true" data-msg-required="Le champs Asynchrone est requis."/> <span data-valmsg-for="async-field" data-valmsg-replace="true"></span> (Ce champ est arrivé après le chargement de la page)');
  }, 10000);
});



/*****************************************\
  Pour forcer la validation d'un formulaire
  arriver dans le DOM après, utiliser.
  « $.validator.unobtrusive.parse('form'); »
\*****************************************/

$(function () {
  setTimeout(function () {
    $(".asynchone-form").html('<form action="" method="post"><label for="async">Asynchrone</label> <input type="text" name="async" data-val="true" data-rule-required="true" data-msg-required="Valider un formulaire asynchrone"/> <span data-valmsg-for="async" data-valmsg-replace="true"></span> <input type="submit" value="Test Asynchrone" /> (Ce formulaire est arrivé après le chargement de la page)<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li></ul></div></form>');
    $.validator.unobtrusive.parse('form');
  }, 10000);
});
              
            
!
999px

Console