I have HTML form with javascript validation but whenever i submit the form error message appear even if the form fields are correct(not empty or null)
I want the error message to appear only when form fields are empty or null when i submit the form
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=0.1,shrink-to-fit=no">
<link type="text/css" href="/css" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Javascript Form Validation</title>
<style>
</style>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-sm-6 col-md-6 col-lg-6">
<div class="card">
<div class="card-body">
<form method="post" name="myForm" id="myForm">
<div class="form-group">
<label for="exampleInputName1">Full name</label>
<input type="text" class="form-control" name="name" id="exampleInputName1" aria-describedby="nameHelp" placeholder="Enter name">
</div>
<button type="submit" name="login" class="btn btn-primary">Submit</button>
</form>
<script>
class User
{
constructor(userName,submitButton)
{
this.userName = document.myForm.name.value;
this.submitButton = document.getElementById("myForm");
}
submitForm()
{
this.submitButton.onsubmit = function()
{
if(this.userName == "" || this.userName == null)
{
document.write("Name Required");
return false;
}
}
}
}
let user = new User();
user.submitForm();
</script>