I have the following code:
我有以下代码:
var inp = $("#txt");
if(inp.val() != "")
// do something
Is there any other way to check for empty textbox using the variable 'inp'
是否有其他方法使用变量“inp”检查空文本框
8 个解决方案
#1
147
if (inp.val().length > 0) {
// do something
}
if you want anything more complicated, consider regex or use the validation plugin which takes care of this for you
如果您想要更复杂的东西,可以考虑使用regex或使用验证插件来为您解决这个问题
#2
91
var inp = $("#txt").val();
if(jQuery.trim(inp).length > 0)
{
//do something
}
Removes white space before checking. If the user entered only spaces then this will still work.
在检查前删除空白。如果用户只输入空格,那么这仍然有效。
#3
16
if ( $("#txt").val().length > 0 )
{
// do something
}
Your method fails when there is more than 1 space character inside the textbox.
当文本框中有超过一个空格字符时,您的方法将失败。
#4
6
$('input:text').filter(function() { return this.value.length > 0; });
#5
6
Use the following to check if text box is empty or have more than 1 white spaces
使用以下命令检查文本框是否为空或有超过1个空格
var name = jQuery.trim($("#ContactUsName").val());
if ((name.length == 0))
{
Your code
}
else
{
Your code
}
#6
4
if ( $("#txt").val().length == 0 )
{
// do something
}
I had to add in the == to get it to work for me, otherwise it ignored the condition even with empty text input. May help someone.
我必须添加==才能让它为我工作,否则即使是空文本输入,它也会忽略条件。可以帮助别人。
#7
2
Also You can use
你也可以使用
$value = $("#txt").val();
if($value == "")
{
//Your Code Here
}
else
{
//Your code
}
Try it. It work.
试一试。它的工作。
#8
2
The check can be done like this:
支票可以这样做:
if (!!inp.val()) {
}
and even shorter:
甚至更短:
if (inp.val()) {
}
#1
147
if (inp.val().length > 0) {
// do something
}
if you want anything more complicated, consider regex or use the validation plugin which takes care of this for you
如果您想要更复杂的东西,可以考虑使用regex或使用验证插件来为您解决这个问题
#2
91
var inp = $("#txt").val();
if(jQuery.trim(inp).length > 0)
{
//do something
}
Removes white space before checking. If the user entered only spaces then this will still work.
在检查前删除空白。如果用户只输入空格,那么这仍然有效。
#3
16
if ( $("#txt").val().length > 0 )
{
// do something
}
Your method fails when there is more than 1 space character inside the textbox.
当文本框中有超过一个空格字符时,您的方法将失败。
#4
6
$('input:text').filter(function() { return this.value.length > 0; });
#5
6
Use the following to check if text box is empty or have more than 1 white spaces
使用以下命令检查文本框是否为空或有超过1个空格
var name = jQuery.trim($("#ContactUsName").val());
if ((name.length == 0))
{
Your code
}
else
{
Your code
}
#6
4
if ( $("#txt").val().length == 0 )
{
// do something
}
I had to add in the == to get it to work for me, otherwise it ignored the condition even with empty text input. May help someone.
我必须添加==才能让它为我工作,否则即使是空文本输入,它也会忽略条件。可以帮助别人。
#7
2
Also You can use
你也可以使用
$value = $("#txt").val();
if($value == "")
{
//Your Code Here
}
else
{
//Your code
}
Try it. It work.
试一试。它的工作。
#8
2
The check can be done like this:
支票可以这样做:
if (!!inp.val()) {
}
and even shorter:
甚至更短:
if (inp.val()) {
}