Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính maxLength của HTML DOM Input Email

Thuộc tính maxLength của đối tượng Input Email lấy hoặc thiết lập số ký tự tối đa cho phép nhập vào trường email. Mặc định trả về -1 (không giới hạn).

Cú pháp

Lấy giá trị maxLength

inputEmailObject.maxLength

Thiết lập maxLength

inputEmailObject.maxLength = số

Ví dụ minh họa

Tăng dần maxLength và kiểm tra đăng nhập:

<!DOCTYPE html>
<html>
<head>
<title>Input Email maxLength</title>
<style>
  form { width:70%; margin: 0 auto; text-align: center; }
  * { padding: 2px; margin:5px; }
  input[type="button"] { border-radius: 10px; }
</style>
</head>
<body>
<form id="Multitech-MNC">
  <fieldset>
    <legend>Email-maxLength</legend>
    <label for="EmailSelect">Email nhân viên: <input type="email" id="EmailSelect" placeholder="jkl@qwerty.com" maxLength="10"> </label>
    <input type="button" onclick="logIN()" value="Đăng nhập">
    <input type="button" onclick="changeMaxLength()" value="Tăng Maxlength">
    <div id="divDisplay"></div>
  </fieldset>
</form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var inputEmail = document.getElementById("EmailSelect");
  divDisplay.textContent = 'Maxlength: ' + inputEmail.maxLength;
  function changeMaxLength() {
    inputEmail.maxLength += 5;
    divDisplay.textContent = 'Maxlength: ' + inputEmail.maxLength;
  }
  function logIN(){
    if(inputEmail.value !== '')
      divDisplay.textContent = 'Đăng nhập vào: ' + inputEmail.form.id;
    else
      divDisplay.textContent = 'Vui lòng nhập email hợp lệ';
  }
</script>
</body>
</html>

Kết quả

Ban đầu maxlength = 10. Nhấn "Tăng Maxlength" cộng thêm 5 (15, 20...). Nhấn "Đăng nhập" với email hợp lệ hiển thị form ID.