Merhaba.javascriptde kullanicin bos mesaj gondermesini engellemek icin soyle bir kod yazdim:
if(document.querySelector("textarea").value == false){
alert("mesaj bos olamaz")
}
else{
document.querySelector("textarea").value = ""
document.querySelector("textarea").focus()
}
ama soyle bir sorun var:kullanici 0 yazinca js dogal olarak(0 falsedir) bunu false kabul ediyor ve mesaji bos algiliyor.bu sorunun cozumu konusunda yardimci olursaniz sevinirim.
tum kodlar:
<!DOCTYPE html>
<html lang="tr-TR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jquery ogrenme</title>
<!-- <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script> -->
<style type="text/css">
body{
margin: 0px;
padding: 0px;
font-weight: bolder;
}
#genel{
width: 300px;
height: 500px;
margin-left: 35%;
}
#genel2{
border: 3px solid gray;
box-sizing: border-box;
width: 307px;
display: none;
}
#send{
width: 300px;
height: 50px;
background-color: rgb(10, 165, 10);
position: relative;
top: 0px;
outline: none;
border-radius: 5px;
box-sizing: border-box;
}
textarea{
resize: none;
box-sizing: border-box;
}
#openclose{
background-color: red;
outline: none;
border-radius: 5px;
width: 300px;
height: 50px;
}
</style>
</head>
<body>
<div id="genel">
<div id="genel2">
<textarea name="" id="" cols="35" rows="11" placeholder="enter message"></textarea>
<button id="send" onclick="send()">Send</button>
</div>
<button id="openclose" onclick="openclose()">Ac</button>
</div>
<script>
function openclose(){
var openbut = document.querySelector("#openclose")
var genel2 = document.querySelector("#genel2")
if(openbut.innerHTML == "Ac"){
openbut.innerHTML = "Bagla"
genel2.style.display = "block"
}
else{
openbut.innerHTML = "Ac"
genel2.style.display = "none"
}
}
function send(){
var valu = document.querySelector("textarea").value
valu = valu.toString()
if(valu== false){
alert("mesaj bos olamaz")
}
else{
document.querySelector("textarea").value = ""
document.querySelector("textarea").focus()
}
}
</script>
</body>
</html>