Event Listeners
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="listners.css">
<title>Document</title>
</head>
<body>
<div class="container">
<h2>Enter Your Name</h2>
<input id="name" type="text" placeholder="Enter Name" onkeyup="register()" >
<!-- <button class="btn" onclick="register()">Enter Button</button> -->
<div id="data"></div>
</div>
<script src="listners.js"></script>
</body>
</html>
// java script file
function register(){
let name=document.getElementById("name")
let data=name.value
let count=data.length
if(count<=3){
name.classList.add('input-danger')
name.classList.remove('input-focus')
document.getElementById('data').innerText="Enter atleast 4 characters"
}
else{
name.classList.add('input-focus')
name.classList.remove('input-danger')
document.getElementById('data').innerText="Good to go"
}
}
// CSS File
body{
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
.container{
width: 400px;
margin: 150px auto;
}
input{
padding: 16px 32px;
border-radius: 10px;
width: 100%;
border: 1.5px solid rgb(170, 170, 170);
}
input:focus{
outline: none;
border-color: royalblue;
}
.btn{
padding: 16px 32px;
font-size: 16px;
border-radius: 10px;
background-color: black;
color: white;
border: none;
margin-top: 20px;
}
#data{
color: red;
margin-top: 20px;
}
.input-focus{
border: 1px solid green;
}
.input-danger{
border: 1px solid red;
}
Output:-
Comments
Post a Comment