io ho fatto così e sembra funzionare:
codice:
<!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>Pagina senza titolo</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function validate(theForm )
{
var valore = $("valore").value;
if(valore == "False")
return true;/*select = Sì: campo File non obbligato*/
else if (valore == "True")
{
var file = trim($("File1").value);
if(file.length == 0)
{
alert("Il campo File è obbligato");
return false;
}
else return true;
}
else
{
alert("Non puoi andare avanti.");
$("valore").focus();
return false;
}
}
//al posto di mettere document.getElementById("div1"), mettere $("div1")
//da prototype.js
function $() {
var elements = new Array();
for (var i = 0; i < arguments.length; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push(element);
}
return elements;
}
function trim(stringa)
{
stringa = stringa + "";
return stringa.replace(/^ */,"").replace(/ *$/,"");
}
// ]]>
</script>
</head>
<body>
<form method="post" action="a.asp" onsubmit="javascript:return validate(this);" id="form1" enctype="multipart/form-data">
<select size="1" id="valore" name="valore">
<option value="">Seleziona</option>
<option value="True">Si</option>
<option value="False">No</option>
</select>
<input type="file" name="File1" id="File1" onkeydown="this.blur();" />
<input type="submit" name="B1" value="Invia" id="B1" />
</form>
</body>
</html>
ma ho notato un problema che mi disturba e forse puoi aiutarmi tu:
se la pagina si chiama a.htm, l'istruzione alert("Il campo File è obbligato"); viene mostrata correttamente.
Se cambio l'estensione del file in asp, file a.asp, l'alert stampa male le lettere accentate
Ne conosci il motivo? :master: