Reputation: 3
This is a guestbook validation which I am trying to make. The text in the <p>
tag is not getting changed and I don't know why. Could you help me to validate this form:
function Frmvalidate() {
var nmchk = document.forms["guestform1"]["name"].value;
var cmntchk = document.forms["guestform1"]["comment"].value;
if (nmchk == null || nmchk == "") {
var namep = document.getElementById("namep");
x.innerHTML = "name must be filled out";
return false;
} else {
return true;
}
if (cmntchk == null || cmntchk == "") {
var cmntp = document.getElementById("cmntp");
x.innerHTML = "comment must be filled out";
return false;
} else {
return true;
}
}
Upvotes: 0
Views: 96
Reputation: 382160
Instead of
var namep = document.getElementById("namep");
x.innerHTML="name must be filled out";
you probably want
var namep = document.getElementById("namep");
namep.innerHTML="name must be filled out";
But when you execute the first if else
, you quit the function, so you probably want to do this :
function Frmvalidate() {
var nmchk=document.forms["guestform1"]["name"].value;
var cmntchk=document.forms["guestform1"]["comment"].value;
if (nmchk==""||nmchk==null) {
var namep = document.getElementById("namep");
namep.innerHTML="name must be filled out";
return false;
}
if (cmntchk==""||cmntchk==null){
var cmntp = document.getElementById("cmntp");
cmntp.innerHTML="comment must be filled out";
return false;
}
return true;
}
Upvotes: 2