表單檢核_方法1(逐一check)

<h2>資料輸入</h2>
<form
id="form1" method="post" action="result.php">
  <p>姓名:
      <input type="text" name="receiver"
id="receiver" />
  </p>
  <p>電話:
      <input type="text" name="phone"
id="phone" />
  </p>
  <p>確認:
      <input type="checkbox" name="sure"
id="sure" />
  </p>
  <p>
    <input type="submit" value="送出" />
  </p>
</form>

html語法

表單檢核_方法1(逐一check)

document.getElementById('form1').onsubmit = function(){ return check_data(); };

function check_data()

{

// 檢查收件者

var receiver = document.getElementById('receiver');

if(receiver.value=='')

{

alert('收件者不得為空白');

return false;

}

 

javascript語法

表單檢核_方法1(逐一check)

 

  // 檢查電話

  var phone = document.getElementById('phone');

  if(phone.value=='')

  {

    alert('電話不得為空白');

    return false;

  }

 

javascript語法

表單檢核_方法1(逐一check)

// 檢查確認勾選

  var sure = document.getElementById('sure');

  if(!sure.checked)

  {

    alert('確認項目必須勾選');

    return false;

  }

 

  // 此處應直接傳送到後端

  alert('各項資料均已正確輸入....');

 

}

javascript語法