I am stuck on how to make a span element become hidden again when the JavaScript validation succeeds. Currently onchange
and onblur
a red span appears showing an error if there is no text or if there are numbers in a name field. This does not disappear when the correct text is put in. I was just wondering how to make this message disappear when the correct text is put in? Code is below.
JavaScript:
function validateName() {
var name = form.firstname.value;
if (form.firstname.value == "") {
document.getElementById("firstnameInvalid").style.visibility = "visible";
return false;
} if (/[0-9]/.test(name)) {
document.getElementById("firstnameInvalid").style.visibility = "visible";
return false;
}
return true;
}
Form HTML:
<form name="form" method="post" action= "userdetails.html" onsubmit="return validate(this)">
<p>First Name:<input type="text" name="firstname" onblur="validateName()" onchange="validateName()" id="name">
<span id="firstnameInvalid" style="color:red; visibility:hidden"> First Name is Invalid </span>
</p>
You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <span> element is not visible, but it maintains its position on the page.
We used the textContent property on the span to change its text content. The textContent property can also be used to read the text content of the element and its descendants. Setting textContent on an element removes all of the element's children and replaces them with a single text node with the provided string.
You can use the HTML span tag as a container to group inline elements together so you can style or manipulate them with JavaScript.
HTML is a very flexible scripting language that allows you to add a lot of things that are not defined in the language, which then will be simply ignored. So according to that, adding an href attribute on an span element is valid, but will not work as a real href attribute and will be simply ignored.
You're on the right track.
Fiddle demo
function validateName() {
var name = form.firstname.value;
if (form.firstname.value == "") {
document.getElementById("firstnameInvalid").style.visibility = "visible";
return false;
} else if (/[0-9]/.test(name)) {
document.getElementById("firstnameInvalid").style.visibility = "visible";
return false;
} else {
document.getElementById("firstnameInvalid").style.visibility = "hidden";
}
}
You can simplify this a bit by using your variable and removing the returns, which don't seem to be necessary:
Fiddle demo
function validateName() {
var name = form.firstname.value;
if (name == "") {
document.getElementById("firstnameInvalid").style.visibility = "visible";
} else if (/[0-9]/.test(name)) {
document.getElementById("firstnameInvalid").style.visibility = "visible";
} else {
document.getElementById("firstnameInvalid").style.visibility = "hidden";
}
}
I would suggest you to use :
style="display:none"
- Hiddenstyle="display:block"
- Visible
HTML:
<p>First Name:<input type="text" name="firstname" onblur="validateName(this.value)" onchange="validateName(this.value)" id="name">
<span id="firstnameInvalid" style="color:red; display:none;"> First Name is Invalid </span></p>
Javascript:
function validateName(name) {
if (name == "")
{
document.getElementById("firstnameInvalid").style.display="block";
return false;
}
else if (/[0-9]/.test(name))
{
document.getElementById("firstnameInvalid").style.display = "none";
return false;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With