以下のコードを使用して、フォームのテキスト ボックスが空であるかどうかを確認しています。
function myFunction(){
var week_no=document.getElementById("week_no").value;
var date_rep=document.getElementById("date_rep").value;
if(week_no==null || week_no=="")
{
alert("week_no field must be filled out");
document.getElementById("week_no").style.background="pink";
document.getElementById("week_no").focus();
return false ;
}
if(date_rep==null || date_rep=="")
{
alert("date_rep field must be filled out");
document.getElementById("date_rep").style.background="pink";
document.getElementById("date_rep").focus();
return false;
}
}
次のように、HTML からこの JavaScript 関数を呼び出します。
<form id="form1" name="form1" method="post" onsubmit="return myFunction()" action="insert_alert.php" >
私のフォームは非常に長いので、変数を作成して個々のフィールドにチェックを適用することができません。誰かが私の労力を減らすために私を導いてくれれば幸いです。