I have made a form as index.html. I have a from validation using JavaScript. When the submit button is clicked it must open another html page(detail.html). When I supply wrong data, it must prevent the opening of detail.html page and display the error messages but neither of them happens .
here is the code
<form action="/detail" method="POST" role="form" name="index" onsubmit="return validateForm()" enctype="application/x-www-form-urlencoded">
<label for="name">Name</label>
<input type="text" name="name" placeholder="Enter your name" required/>
<span class="formerror"></span>
<br />
<label for="phone_number">Mobile:</label>
<input type="number" name="phone_number" placeholder="enter your mobile number" required >
<span class="formerror"></span>
<br/>
<label for="password">Password:</label>
<input type="password" name="password" placeholder="enter your password" required />
<span class="formerror"></span>
<br />
<button id="btnsubmit" class="btn" onclick="submit">Submit</button>
code for form validation
<script>
function clearErrors(){
errors=document.getElementsByClassname('formerror');
for(let item of errors){
item.innerHTML="";
}
}
function seterror(id,error){
//sets error inside tag of id
element=document.getElementById(id);
element.getElementsByClassName('formerror')[0].innerHTML=error;
}
function validateForm(){
var returnval = true;
//performs validation and if validation fails, set the value of returnval to false.
var name = document.forms['index']["name"].value;
if(name.length<3)
{
seterror("name","length of name of too short");
returnval=false;
}
var phone_number = document.forms['index']["phone_number"].value;
if(phone_number.length!=10)
{
seterror("phone_number","phone number should be of 10 digits");
returnval=false;
}
var password = document.forms['index']["password"].value;
if(password.length<4)
{
seterror("password","password should be at least of 4 characters");
returnval=false;
}
return returnval;
}
</script>
I wanted the index.html to display the error messages when wrong data is fed to the input fields, but nothing of those happens instead detail.html page opens. detail.html must only open when correct input is given.
2
Answers
You can make your code easier to overview if you get rid of the validate function and use event listeners instead.
The
invalid
event listener will fire when the form is submitted and one or more input elements are not validating. I added attributes on each input element to validate against (likeminlength="3"
for the name andpattern="[0-9]{10}"
for the telephone number). If an input element is not valid I set theinvalid
className. And that results in the form error text to show.When starting to type in an input the input event is fired, and the className is removed again.
I changed your validation to use a sibling error message selected by a class selector, added some classes and CSS for that. Not perfect but shows how. Here I used the change event to clear the error.
Note I also added some validation attributes like
minlength
etc.Many areas can be improved and made more robust here.