Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make span element hidden when javascript validation succeeds

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>
like image 657
TheAnt. Avatar asked Apr 15 '14 02:04

TheAnt.


People also ask

How can I make my span invisible?

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.

Does span have textContent?

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.

Can you use span in JavaScript?

You can use the HTML span tag as a container to group inline elements together so you can style or manipulate them with JavaScript.

Can we use HREF in span tag?

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.


2 Answers

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";
    }
}
like image 58
isherwood Avatar answered Nov 14 '22 21:11

isherwood


I would suggest you to use :

style="display:none" - Hidden
style="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;
}
like image 20
Fariz Azmi Avatar answered Nov 14 '22 21:11

Fariz Azmi