1

I want to validate user input when the user clicked on the submit button, but it won't work.

jQuery

$(document).ready(function() {
   function validateForm() {
      if ($('#u').value() == '') 
      {
         alert("no value found");
         return false;
      }

  });

});

HTML

<form name="myForm" action="main.php" onsubmit="return validateForm()" method="post">
   <input type="text" id="u" />
   <input type="submit" value="submit" />
</form>
4
  • There's no id="u" in your form. Commented Jul 30, 2013 at 3:24
  • What do you mean by "it won't work"? Commented Jul 30, 2013 at 3:37
  • just $('#u').val() . And you'll get it Commented Jul 30, 2013 at 3:41
  • if script is fine then try to replace $ with jQuery many time there is possible confliction with jQuery Commented Jul 30, 2013 at 3:53

4 Answers 4

1

There are many problems in your code.

  • The value is not a function but attribute remove you can use val() and also you can not use value with jQuery object you can use it with DOM object.

  • You have probably forgot to assign id to your input field also assign that.

  • The statement <input type="text"> should b4 <input id="u" type="text">

  • You have extra parenthesis after closes currly bracket of function validateForm, you are not closing input tags.

Live Demo

if ($('#u').val() == '') 
{
   alert("no value found");
   return false;
}

You can use native javascript method getElementById to get the DOM element (object) and use value property of that object.

Live Demo

if (document.getElementById('u').value == '') 
{
     alert("no value found");
     return false;
}
Sign up to request clarification or add additional context in comments.

4 Comments

True, but there is no id="u", so your answer doesn't work for his particular problem. EDIT: Now the question includes id="u", he just modified it. Lol.
I want to change the value of #label instead of alert. Thats why i used jquery
You can do that with javascript as well, as for as I know, I could not see any label in your html
Check my updated demo with both jQuery and javascript with changing label instead of alert.
0

Try

if ($('#u').value() == '') 
{
    alert("no value found");
    return false;
} else {
    $('form[name="myForm"]').submit();
}

Comments

0

As @Barmar suggested check your existing code for the missing id. I would add the id="u" to the <input type="text"> and also be careful in choosing id names that are ambiguous especially so that others could read your code later.

Now that you've edited the original question it is hard to see how my answer appears relevant

Comments

0

By the way, I found an awkwardness in your code. Let's focus to this line :

  ......
   if ($('#u').value() == '') 
   {
     alert("no value found");
     return false;
   }

});

});

There's two });, which one for $(document) and which one for if() ?? Although for if(), don't type like that, just } for conditional closing. And for make a function ..., you don't need to put function inside $(document).ready. It can stand-alone. For example :

function validateForm() {

   if ($('#u').val() == '') 
   {
      alert("no value found");
      return false;
   }

}

And you should change .value() to .val()

See this for the result. Hope this helps

Comments

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.