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
| <!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>
<title>activer un bouton si le formulaire est correctement saisi</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<style type="text/css">
</style>
</head>
<body>
<form id="f" action="p.html" method="post">
<p><label for="nom">nom</label><input id="nom" name="nom" type="text" /></p>
<p><label for="prenom">prenom</label><input id="prenom" name="prenom" type="text" /></p>
<p><label for="email">email</label><input id="email" name="email" type="text" /></p>
<button id="b" type="submit" value="v" disabled="disabled">valider</button>
</form>
<script type="text/javascript">
<!--
var FC = {
Evenement: {
ajouter: function(elm, evt, fn) {
if (document.addEventListener) {
if (elm.nodeType===1 || elm.nodeType===9) {
elm.addEventListener(evt, function(e) {
if (!fn(e)) { e.preventDefault(); }
}, false);
} else {
for (var i = 0, imax = elm.length; i < imax; i++) {
elm[i].addEventListener(evt, function(e) {
if (!fn(e)) { e.preventDefault(); }
}, false);
}
}
} else if (document.attachEvent) {
if (elm.nodeType===1 || elm.nodeType===9) {
return elm.attachEvent("on" + evt, fn);
} else {
for (var i = 0, imax = elm.length; i < imax; i++) {
elm[i].attachEvent("on" + evt, function(e) {
if (!fn(e)) { e.returnValue = false; }
});
}
}
}
}
},
Validation: {
Flag: true,
estSaisi: function(elm) {
if(elm.value==="") { FC.Validation.Flag=false; }
},
estEmail: function(elm) {
var p = /\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/ig;
if(elm.value.match(p)) { FC.Validation.Flag=false; }
}
}
};
FC.Evenement.ajouter(document.getElementsByTagName("input"), "keyup", function(e) {
var elm = e.target || event.srcElement;
if(elm.type==="text") {
if(elm.id==="email") { FC.Validation.estEmail(elm); }
else { FC.Validation.estSaisi(elm); }
if(FC.Validation.Flag) { document.getElementById("b").disabled=false; }
}
});
//-->
</script>
</body>
</html> |
Partager