
<!By GadTool>
<!DOCTYPE HTML>
<!By https://gadtool.blogspot.com>
<html>
<script>
function check(){
var check=document.getElementById("GadTool_input").value;
if(check==""){
//by gadtool.blogspot.com
document.getElementById("result").innerHTML = "Input field can not be empty!!";
alert("Input field can not be empty!!");
}else {
document.getElementById("result").innerHTML = "";
}
}
</script>
<input id="GadTool_input"/>
<br><button onclick="check()">Run Check</button>
<p id="result" style="color:red"></p>
</html>
<!DOCTYPE HTML>
<!By https://gadtool.blogspot.com>
<html>
<script>
function check(){
var check=document.getElementById("GadTool_input").value;
if(check==""){
//by gadtool.blogspot.com
document.getElementById("result").innerHTML = "Input field can not be empty!!";
alert("Input field can not be empty!!");
}else {
document.getElementById("result").innerHTML = "";
}
}
</script>
<input id="GadTool_input"/>
<br><button onclick="check()">Run Check</button>
<p id="result" style="color:red"></p>
</html>
Explanation
First of all, we have added input to take input, here number, from the user. In the above example, we have given " GadTool_input " as the id to the input . Then, we have added a button , which on getting clicked, triggers the function check() in JavaScript to check whether the input is empty or not.
JavaScript Part
At first, we have added a function check() that gets triggered every time the user clicks on the button reading 'Run Check'. Inside the function, we initialized a variable check to carry the value of the input . Then, we have added an if else statement to run a check on the value of the input .if else Statement
In the beginning of the statement, we have made the statement to check whether the value of the variable check is empty(""). If the value of the variable check is found to be empty then we made the statement to run the if part and if the the value of the variable check is found to not be empty, then we made the statement to run the else part.➤ if part :- If the value of the variable check is found to be empty then the if part starts executing. In the if part, we have made it to print that the input field is empty in the element with the id as result and we have also added a code to alert the same statement.
➤ else part :- If the value of the variable check is found not to be empty then the else part starts executing. We have made the else part to empty every text written in the p (paragraph) with the id as result.
At last of this we have closed the function check().
0 Comments