Sự kiện onkeypress gán trực tiếp trong JavaScript

Sự kiện onkeypress gán trực tiếp trong JavaScript

Sự kiện onkeypress gán trực tiếp trong JavaScript

Tự code rồi chạy thử... Chạy Thử
<!DOCTYPE html>
<html>
<head>
<title>Sự kiện onkeypress gán trực tiếp trong JavaScript</title>
</head>
<body>

<h1>Sự kiện onkeypress gán trực tiếp trong JavaScript</h1>
<p>Dùng để xử lý sự kiện khi gõ một phím bất kì vào ô input của thẻ HTML:</p>

<input onkeypress="nhapTuoi()" type="text" id="tuoi" placeholder="Nhập tuổi từ 18 tới 40" ><br>
<script>
function nhapTuoi(){
var tuoi= document.getElementById('tuoi');
var sotuoi =  tuoi.value;
if(sotuoi<18||sotuoi>40){
  alert("Bạn phải nhập tuổi từ 18 tới 40"); 
}
}
</script>
</body>
</html>
Sự kiện onkeypress gán trực tiếp trong JavaScript nằm trong bài viết Sự kiện onkeypress trong JavaScript