-1

I am trying to check if the name text field is empty. if it is, then i want to show a div that says "please enter your name". i do a test, and name='name'. but after that, nothing else pops up.

function validate(input, name) {
if(name=='name') {
        if(input==null || input=="") {
            name.style.visibility='visible';
            alert("please enter a value");
        } else {
            alert("valid entry.");
        }
    }
}

when I print out input with the alert box, it does not contain anything; so I know that it is either null or "". how do i fix this so that it works?

here is my text field:

<input type="text" name="name" id="name" onblur="javascript: validate(this.value, this.id);" required />
    <div id="name" style="visibility:hidden; float:right;">Please fill in a name!</div>
6
  • 4
    name == 'name' makes no sense when it's referenced later as an object. Commented Apr 25, 2012 at 23:29
  • 3
    Show your HTML, show where the variable name is being assigned. And then show us a representative demo; we're looking for SSCCE (Short, Self-Contained, Correct/Compilable Example). Commented Apr 25, 2012 at 23:29
  • Open up your browser's javascript console Commented Apr 25, 2012 at 23:30
  • Can you post where input is being set? And can you try to comment out name.style.visibility and see if that produces an alert? Commented Apr 25, 2012 at 23:31
  • @tehulz odd, that worked! how come? Commented Apr 25, 2012 at 23:32

1 Answer 1

1

The javascript: protocol is only used when you have Javascript in an URL, don't use it in event handlers.

You can't have two elements with the same id. The id has to be unique.

In XHTML there are no attributes without a value. You should use required="required" rather than just required.

You don't have to check the value for null. The value of a text field is never null.

Use the getElementById method to find an element from its id.

function validate(input, name) {
  if (name == 'name') {
    if (input == '') {
      document.getElementById('nameInfo').style.visibility = 'visible';
      alert("please enter a value");
    } else {
      alert("valid entry.");
    }
  }
}

<input type="text" name="name" id="name" onblur="validate(this.value, this.id);" required="required" />
<div id="nameInfo" style="visibility:hidden; float:right;">Please fill in a name!</div>
Sign up to request clarification or add additional context in comments.

3 Comments

+1, but what makes you think the OP is using XHTML? There is no DOCTYPE and real XHTML is extremely rare on the web. XML-like markup is not a reliable indicator. Better to tell the OP to remove the faux SHORTAGs.
@RobG: It's based on the self closed input tag. Hopefully the OP knows whether he is using XHTML or not, so that he knows whether that point is relevant.
i used just required because i saw it in html 5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.