使用jQuery或JavaScript检查输入/类型文本是否为空/空 [英] Checking Input/Type Text is empty/null with jQuery or JavaScript
问题描述
我很困惑.我正在尝试检查我的JavaScript中输入的type:text是否为空或为null.我目前有这个:
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.");
}
我尝试过:.val() === ""
,.val() == ""
,.length() === 0
,.length() == 0
,.length() < 1
,.val().length() === 0
,val().trim().length() < 1
,.trim().length() < 1
,.text() === ""
,.text() == ""
,!$("#startDate").val()
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()
我的HTML是:
<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>
我也尝试过这个:
$('input[class^="dateValidate"]').each(function () {
if($(this).val().trim().length === 0)
{
alert("Please fill out the required fields.");
}
我的想法不多了.
****更新****
****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();
}
我尝试过:
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
推荐答案
似乎选择器中的id映射错误startDate
.在您的代码中,您具有诸如startDate_[pk]
之类的ID.因此,您需要一个开头为的选择器,如下所示.
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
这篇关于使用jQuery或JavaScript检查输入/类型文本是否为空/空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!