ป้องกัน Copy Paste

Byphunsanit

ป้องกัน Copy Paste

ถ้าต้องการไม่ให้ user คัดลอกข้อมูลมาวางในช่องยืนยันอีเมล์ เราสามารถใช้คุณลักษณะ (attribute) onpaste ของเท็ก อินพุตช่วยป้องกันได้ ตามตัวอย่าง

<input type="text" name="confrimEmail" autocomplete="off" onDrag="return false;" onDrop="return false;" onPaste="return false;" />

แต่เนื่องจากโอเปร่าไม่สนับสนุนความสามารถนี้ (ตัวเดียว ?) ทำให้ต้องใช้จาว่าสคริปต์เต็มรูปแบบในการตรวจสอบ

<input type="text" name="confrimEmail" autocomplete="off" onDrag="return false;" onDrop="return false;" onPaste="return false;" onMouseDown="noRightClick(event);" onKeyDown="return noPaste(event);" />
<script>
function noPaste(event)
{
	var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
	if (event.ctrlKey && pressedKey == 'v')
	{
		alert('Sorry, this functionality is disabled.');
		return false;
	}
}

function noRightClick(event)
{
	if (event.button==2)
	{
		alert("Right Click Not Allowed!");
	}
}
</script>

มี 2 ฟังก์ชั่นคือ

  1. noRightClick ป้องกันการคลิกขวาทุกชนิด
  2. noPaste ป้องกันการวางโดนใช้ชอร์ตคัต Shortcut,ชอร์ตคัต คนที่เคยเขียนเกมส์มาจะห็นว่าความจริง เราใช้แค่ event.ctrlKey ก็ตรวจจับการใช้ ปุ่ม control ได้แล้ว ภาษาหรือเครื่องแมค บางครั้งต้องใช้ปุ่มนี้ในการพิมพ์อักษรบางตัว ทำให้ต้องใช้ event.keyCode มาจับว่าเป็นการวางข้อความ Ctrl + V รึเปล่า

ว่างๆ ลองดัดแปลงเป็นตรวจจับการจับภาพหน้าจอหรือพิมพ์หน้าเว็บดูนะครับ
ดูเพิ่มเติม

About the author

phunsanit administrator

    Leave a Reply