Solution 1 :

I notice that you have some lower case error in your validateform function

function validateform() {
  var x = document.getElementByid("fname").value; // here should be getElementById
  if (x.length < 1) {
    alert("Name can't be blank");
    return false;
  }
}

I try this and it works on code snippet.

Solution 2 :

The blocking point was in my css scroll-behaviour. I disabled it. Now the validation rules are working properly.

html{
font-size: 62.5%;
overflow-x: hidden;
/scroll-padding-top: 6rem;
scroll-behavior: smooth;
/
}

Problem :

I have set the below logic to validate the form input but no alert box is being displayed on submit. Please note that the issue is not related to the setCustomvalidity function. Rather is it related to the alert box not being displayed.

function validateform() {
  var x = document.getElementByid("fname").value;
  if (x.length < 1) {
    alert("Name can't be blank");
    return false;
  }
}
<div class="row">
        <div class="col-25">
          <label for="fname">First Name *</label>        
        </div>
        <div class="col-75">
          <input type="text" id="fname" name="firstname"   required
          oninvalid="this.setCustomValidity('This field cannot be left blank')"
          oninput="this.setCustomValidity('')">        
        </div>
      </div>

<form name="bookingform" action="bookingform.php" method="post" onsubmit="return validateform()">

Comments

Comment posted by Ash

Hello, thank you for the response. In fact I have tested it section by section. I have noticed that when several form items are entered so that we need to scroll down the form, the validation rule does not work. The validation rules only works when the complete form and submit button occupies the screen. Do you know how we can solve it?

Comment posted by dollar

Would you mind post more details so that we can figure out what cause the error ?

By