Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a specific textbox under a specific div is empty

I have this code:

<PlaceHolder>
<div class="greenDiv">
  <asp:TextBox ID="a"  runat="server" />
</div>
<div  class="greenDiv">
  <asp:TextBox ID="ab" runat="server" />
</div>
</PlaceHolder>

I need to let the user know if he left a Textbox empty, I tried something like this.. (not working) What have I missed?

 $('.greenDiv > input:text').blur(function () {
   if (!$(this).val()) {
     alert("fill this field");
   }
 });
like image 980
Damkulul Avatar asked Dec 16 '14 14:12

Damkulul


2 Answers

Try using input[type=text] instead

$('.greenDiv>input[type=text]').blur(function () {
    if (!$.trim($(this).val())) {
        alert("fill this field");
    }
});
like image 76
MH2K9 Avatar answered Nov 14 '22 22:11

MH2K9


$('.greenDiv > input[type=text]').on('blur', function (e) {
  if (!$(e.currentTarget).val()) alert('fill this field');
});
like image 41
Evgeny Samsonov Avatar answered Nov 14 '22 22:11

Evgeny Samsonov