Skip to content. Skip to navigation
You are here: Home Base de connaissances sur l'accessibilité Les règles d'accessibilité Les règles 1.0 Exemple de formulaire avec mise à jour dynamique

Exemple de formulaire avec mise à jour dynamique

by Jean-Marie D'Amour last modified 2009-03-11 09:47

Télécharger : Exemple de formulaire avec mise à jour dynamique

Size 5.0 kB - File type text/html

File contents

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans nom</title>
<script type="text/javascript">
 window.onload = initialise;
 function initialise()
 {
   // Ensure we're working with a relatively standards compliant user agent
    if (!document.getElementById || !document.createElement || !document.createTextNode)
        return;
    // Add an event handler for the number form
    var objForm = document.getElementById('numberform');
    objForm.onsubmit= function(){return validateNumbers(this);};
 }
  // Function to create a list item containing a link describing the error
 // that points to the appropriate form field
 function addError(strError, strFragment, objForm, strID)
 {
   var objAnchor = document.createElement('a');
   var objListItem = document.createElement('li');
   objAnchor.appendChild(document.createTextNode(strError));
   objAnchor.setAttribute('href', strFragment);
  
   objAnchor.onclick = function(event){return focusFormField(this, event, objForm);};
   objAnchor.onkeypress = function(event){return focusFormField(this, event, objForm);};
   // If strID has a value, this is the first error in the list
   if (strID.length > 0)
   {
      objAnchor.setAttribute('id', strID);
      objListItem.appendChild(objAnchor);
      return objListItem;
   }
 }

 // Function to place focus to the form field in error
 function focusFormField(objAnchor, objEvent, objForm)
 {
   // Allow keyboard navigation over links
   if (objEvent && objEvent.type == 'keypress')
      if (objEvent.keyCode != 13 && objEvent.keyCode != 32)
         return true;
   // set focus to the form control
   var strFormField = objAnchor.href.match(/[^#]\w*$/);
   objForm[strFormField].focus();
   return false;
 }

function validateNumbers(objForm)
 {
   // Test whether fields are valid
   var bFirst = isNumber(document.getElementById('num1').value);
   var bSecond = isNumber(document.getElementById('num2').value);
   // If not valid, display errors
   if (!bFirst || !bSecond)
   {
      var objExisting = document.getElementById('validationerrors');
      var objNew = document.createElement('div');
      var objTitle = document.createElement('h2');
      var objParagraph = document.createElement('p');
      var objList = document.createElement('ol');
      var objAnchor = document.createElement('a');
      var strID = 'firsterror';
      var strError;
      // The heading element will contain a link so that screen readers
      // can use it to place focus - the destination for the link is 
      // the first error contained in a list
      objAnchor.appendChild(document.createTextNode('Erreurs dans la submission du formulaire'));
      objAnchor.setAttribute('href', '#firsterror');
      objTitle.appendChild(objAnchor);
      objParagraph.appendChild(document.createTextNode('Veuillez prendre note des erreurs suivante et les corrigez'));
      objNew.setAttribute('id', 'validationerrors');
      objNew.appendChild(objTitle);
      objNew.appendChild(objParagraph);
      // Add each error found to the list of errors
      if (!bFirst)
      {
         strError = 'Veuillez mettre une valeur numérique dans le premier champs';
         objList.appendChild(addError(strError, '#num1', objForm, strID));
      }
      if (!bSecond)
      {
         strError = 'Veuillez mettre une valeur numérique dans le deuxième champs';
         objList.appendChild(addError(strError, '#num2', objForm, strID));
      }
	  if (!bFirst || !bSecond)
	  strID = '';
      // Add the list to the error information
      objNew.appendChild(objList);
      // If there were existing errors, replace them with the new lot,
      // otherwise add the new errors to the start of the form
      if (objExisting)
         objExisting.parentNode.replaceChild(objNew, objExisting);
      else
      {
         var objPosition = objForm.firstChild;
         objForm.insertBefore(objNew, objPosition);
      }
      // Place focus on the anchor in the heading to alert
      // screen readers that the submission is in error
      objAnchor.focus();
      // Do not submit the form
      objForm.submitAllowed = false;
      return false;
   }
 }
 // Function to validate a number
 function isNumber(strValue)
 {
   return (!isNaN(strValue) && strValue.replace(/^\s+|\s+$/, '') !== '');
 } 

 
</script>
</head>

<body>
<h1>Formulaire Validation</h1>

 <form id="numberform" method="post" action="form.php">
 <fieldset>
 <legend>Champs numérique</legend>
 <p>
 <label for="num1">Entrer le premier nombre</label> 
 <input type="text" size="20" name="num1" id="num1" />
 </p>
 <p>
 <label for="num2">Entrer le deuxième nombre</label> 
 <input type="text" size="20" name="num2" id="num2" />
 </p>
 </fieldset>
 <p>
 <input type="submit" name="submit" value="Envoyez le Formulaire" />
 </p>
 </form>
</body>
</html>