I am validating a text box using javascript, now what happens is that if the text box = ” or in other words is empty, then a message appears stating “Please Enter in a Room Number”, if not then display an empty string “”.
Now this is fine if the text box is empty but if I type in a space or enter for example and then submit the form, then the validation message does not appear in this instance.
How is it supposed to be coded so that if a textbox is empty of contains whitespaces, then it should display a message?
Below is my code:
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Create a Session</title>
<script type="text/javascript">
function validation() {
var roomTextO = document.getElementById("room");
var errRoomMsgO = document.getElementById("roomAlert");
if (roomTextO.value == ""){
errRoomMsgO.innerHTML = "Please Enter in a Room Number";
}else{
errRoomMsgO.innerHTML = "";
}
}
</script>
</head>
<body>
<form action="create_session.php" method="post" name="sessionform">
<p><strong>8: Room:</strong> <input type="text" id="room">
<br/><span id="roomAlert"></span></p> <!-- Enter Room here-->
</form>
</body>
You can trim the contents of the textbox first: