1

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>
2
  • 1
    JavaScript is case-sensitive, hence there should be an error in the console -> .getElementById() Commented Jul 29, 2022 at 11:53
  • Press F12 to open your browser's debugging tools. When you click the button, are there any errors on the browser console? Is the page re-loading entirely? Commented Jul 29, 2022 at 11:54

2 Answers 2

1

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');
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.