Home > Net >  JS function onclick is not working. It is not calling validations
JS function onclick is not working. It is not calling validations

Time:07-30

Below is function and it is not working. Maybe some small bug in the code which I am unable to identify.

<!DOCTYPE html>
<script>
function check(){
  firstname=document.getelementById("firstname").value;
  lastname=document.getelementById("lastname").value;
  if(lastname==""){
    alert('please enter lastname');
  }
}
</script>

<form action="" method="post" onSubmit="check();">
            <input type="text" id="firstname" name="firstname" ><br>
            <input type="text" id="lastname" name="lastname" ><br>
            <input type="submit"  value="submit">
</form>
</body>
</html>

CodePudding user response:

GetElementById, element with a capital E
JavaScript is case sensive. TIP: use developer options in your browser. They usualy give an error message.

<!DOCTYPE html>
<script>
function check(){
  firstname=document.getElementById("firstname").value;
  lastname=document.getElementById("lastname").value;
  if(lastname==""){
    alert('please enter lastname');
  }
}
</script>

<form action="" method="post" onSubmit="check();">
            <input type="text" id="firstname" name="firstname" ><br>
            <input type="text" id="lastname" name="lastname" ><br>
            <input type="submit"  value="submit">
</form>
</body>
</html>

CodePudding user response:

The bug in the code: case sensitive getelEmentById. Please below code and test it

function check(){
  firstname=document.getElementById("firstname").value;
  lastname=document.getElementById("lastname").value;
  if(lastname==""){
    alert('please enter lastname');
  }
}
  • Related