假定我要處理的form中的內容如下(在此我簡化了一下):
<form name="myform">
<input type="text" name="goodsName" size="8"><br>
<input type="file" name="picture" ><br>
<input type="text" name="price" size="5"><br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>
下面是用JS來驗證輸入數據的合法性:
<script language="javascript">
function mycheck(){
if(myform.goodsName.value==""){
alert("請輸入商品姓名");
myform.goodsName.focus();
return;
}
if(myform.picture.value==""){
alert("請輸入圖片文件的路徑");
myform.picture.focus();
return;
}
if(myform.price.value==""){
alert("請輸入價格");
myform.price.focus();
return;
}
myform.submit();
}
</script>