salve >,< sono novizio di javascript e ho provato a fare uno codice che controlla un determinato form e vede se è stato inserito un valore che entra nei parametri scelti da me ma non funge D=
vi scrito i codici qua sotto >,<
code principale
Codice HTML:
<!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=utf-8" />
<title>Documento senza titolo</title>
<script type="text/javascript" src="numero.js"></script>
</head>
<body>
<form name="formexample" id="formexample" action="#">
<div id="citydiv">inserisci un numero compreso tra 1 e 100: <input id="num" name="num" /> <span id="feedback"> </span></div>
<div><input id="submit" type="submit" /></div>
</form>
<script type="text/javascript">
function init () {
document.form[0].onsubmit = function() { return checkValid() };
}
window.onload = init;
</script>
</body>
</html>
numero.js
Codice HTML:
// JavaScript Document
function checkValid() {
try {
var numField = document.forms[0]["num"];
if (isNaN(numField.value)) {
var err = new Array("non è un numero",numField);
throw err;
}
else if (numField.value > 100) {
var err = new Array("hai messo un numero superiore a 100",numField);
throw err;
}
else if (numField.value < 1) {
var err = new Array("hai messo un numero minore di 1",numField);
throw err;
}
return true;
}
catch(errorObject) {
var errorText = document.createTextNode(errorObject[0]);
var feedback = document.getElementById("feedback");
var newspan = document.createElement("span");
newspan.appendChild(errorText);
newspan.style.color = "#FF0000";
newspan.style.fontWeight = "bold";
newspan.setAttribute("id","feedback");
var parent = feedback.parentNode;
var newChild = parent.replaceChild(newspan,feedback);
errorObject[1].style.background = "#FF0000";
return false;
}
}
che errore ho fatto? (probabilmente un centinaio xD)