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
10
  • Can you post HTML as well ? Commented Jul 6, 2015 at 19:49
  • What's the problem with the one's you've tried? Are they not working? Commented Jul 6, 2015 at 19:50
  • they aren't hitting the alert. it's just failing with no error. Commented Jul 6, 2015 at 19:50
  • try this if($("#startDate").val().trim()) this condition should take care of falsy string (null or undefined or empty) Commented Jul 6, 2015 at 19:50
  • One thing: $("#startDate").val().trim().length === 0, length is not a function, but property. But I guess this is typo since you are saying there are no errors. Commented Jul 6, 2015 at 19:51

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

6 Comments

it hits three times because there are three tags with startDate as part of the name
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.
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
@KevinFischer : updated again. Didn't know this requirement :)
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
|
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())

5 Comments

I also tried that one too. Forgot to add that. Will add now
@KevinFischer, then it means there is something else going on in your code. I you look at the snippet in my answer, it is working
@KevinFischer, see my updated answer, you were not targetting the id correctly for the given html
the [pk] actually changes because it's a guid that gets loaded up on the page load
it's not hitting the alert when there's nothing in the input box
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>

7 Comments

it didn't hit the alert nor did it hit my else
@KevinFischer whats the rest of your jquery
` function validate() { //if ($("#tShowingType").val() === "" || ($("#startDate").val().length === 0) || ($("#startTime").val() === "") || ($("#endTime").val() === "")) if (!$('[id^="startTime"]').val())// || ($("#startTime").val() === "") || ($("#endTime").val() === "")) { alert("Please fill out the required fields."); } else { UI.goToConfirmStep(); } } ` it's a simple function. the ending result will combine those other input fields to create one if statement to validate
when is validate called though?
<button onclick="validate();">Click Me</button>
|
0

Change the condition to the following:

if(!$('[id^="startDate"]').val()){
        alert("Please fill out the required fields.");
    }

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.