1

I'm stumped. I'm trying to check to see if my input type:text is empty, or null, in my JavaScript. I currently have this:

        if ($("#startDate").val().trim().length() === 0)
    {
        alert("Please fill out the required fields.");
    }

I have tried: .val() === "", .val() == "", .length() === 0, .length() == 0, .length() < 1, .val().length() === 0, val().trim().length() < 1, .trim().length() < 1, .text() === "", .text() == "", !$("#startDate").val()

My HTML is:

<fieldset>
<label for="startDate_[pk]" style="display:block; width:100%;text-align:left">Start Time</label>
<input type="text" name="startDate_[pk]" id="startDate_[pk]" style="width:75px;display:inline" placeholder="pick date" />
@@
<select name="startTime_[pk]" id="startTime_[pk]" style="display:inline;" disabled="disabled">
@{
var time = DateTime.Now.Date.AddHours(8);
for (int i = 480; i < 1260; i += 15)
{
<option value="@i">@DateTime.Now.Date.AddMinutes(i).ToShortTimeString()</option>
}
}
</select>
</fieldset>

I have also tried this:

        $('input[class^="dateValidate"]').each(function () {
        if($(this).val().trim().length === 0)
        {
            alert("Please fill out the required fields.");
        }

I'm running out of ideas.

****Update****

The validation works for one selected object. But when you select more than one, the validation only works for the first object. Here's my code:

    function validate()
{
    var startDate = $('[id^="startDate"]').filter(function(){
        return $(this).val().length!==0;
    });
    var showingType = $('[id^="tShowingType"]').filter(function () {
        return $(this).val() === "";
    });
    var startTime = $('[id^="startTime"]').filter(function () {
        return $(this).val() === "";
    });
    var endTime = $('[id^="endTime"]').filter(function () {
        return $(this).val() === "";
    });

    if (startDate.length == 0 || showingType.val() === "" || startTime.val() === "" || endTime.val() === "")
    {
        alert("Please fill out the required fields.");
    }
    else
    {
        UI.goToConfirmStep();
    }

I have tried:

var startDate = $('[id^="startDate"]').filter(function(){
        return $(this).val().length!==0
var startDate = $('[id^="startDate"]').filter(function(){
        return $(this).val().length===0
startDate.length == 0
startDate.length < 0
startDate.length < 1
Kevin Fischer
  • 371
  • 7
  • 16

4 Answers4

1

It seems that you have wrong id mapped- startDate in the selector. In your code you have IDs like startDate_[pk]. So you need a starts with selector like below.

 var emptyInputs= $('[id^="startDate"]').filter(function(){
     return $(this).val().length===0;
 });

if(emptyInputs.length>0){
   alert("Please fill out the required fields.");
}

Fiddle Demo

Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
  • it hits three times because there are three tags with startDate as part of the name – Kevin Fischer Jul 06 '15 at 20:17
  • and i can't really change the name at this point, without changing a ton of other code. i think i could try a class and search for that. – Kevin Fischer Jul 06 '15 at 20:18
  • okay, now it's not working with multiple input fields with different guids attached. Example: I select one and the validation works. but when I select more then one, the validation only works for the first one and not the rest – Kevin Fischer Jul 06 '15 at 20:44
  • @KevinFischer : updated again. Didn't know this requirement :) – Amit.rk3 Jul 06 '15 at 20:49
  • honestly, didn't think it would change much! testing it now. I actually adjusted it to emptyInputs.length < 0 in my testing code. i have updated my question up top for more information – Kevin Fischer Jul 06 '15 at 20:51
  • I have updated the question with something of the things I have tried. It's still validating the first object perfectly (YAY!), but unfortunately, it doesn't validate any of the following objects. – Kevin Fischer Jul 06 '15 at 22:05
0

You are not targetting the id correctly

if(!$("#startTime_[pk]").val())

if (!$('#startTime_[pk]').val()) {
  console.log('input empty');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input id="startTime_[pk]" type="text" value="" />

Update

the [pk] actually changes because it's a guid that gets loaded up on the page load

Then use the starts with selector

if(!$('[id^="startTime"]').val())
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
0

length is not a function. Here is an example of the above code working by removing the parenthesis after length

 function validate() {
   if ($("#tShowingType").val() === "" || ($("#startDate").val().trim().length === 0) || ($("#startTime").val() === "") || ($("#endTime").val() === "")) { 
     alert("Please fill out the required fields."); } else { UI.goToConfirmStep();
    }
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type='text' id='startDate' />
<button onclick='validate();' >Button</button>
depperm
  • 10,606
  • 4
  • 43
  • 67
0

Change the condition to the following:

if(!$('[id^="startDate"]').val()){
        alert("Please fill out the required fields.");
    }
Luke
  • 11,426
  • 43
  • 60
  • 69
Sudhansu Choudhary
  • 3,322
  • 3
  • 19
  • 30