admin管理员组文章数量:1394228
How do I test for a input[text] field that has nothing in?
This is what I have so far:
if ($('#StartingPrice').length == ""){
alert("ERROR!");
}
Any help would be greatly appreciated, Thanks
How do I test for a input[text] field that has nothing in?
This is what I have so far:
if ($('#StartingPrice').length == ""){
alert("ERROR!");
}
Any help would be greatly appreciated, Thanks
Share Improve this question edited Apr 21, 2011 at 15:58 JaredMcAteer 22.5k5 gold badges51 silver badges66 bronze badges asked Apr 21, 2011 at 15:56 NasirNasir 4,86511 gold badges36 silver badges39 bronze badges7 Answers
Reset to default 8try this:
if ($('#StartingPrice')[0].value.length == 0){
alert("ERROR!");
}
$('#StartingPrice').length
returns an integer so it will never equal ""
.
Try using the val()
method:
if($('#StartingPrice').val() == "")
{
alert("ERROR!");
}
.length
The number of elements in the jQuery object.
.val()
Get the current value of the first element in the set of matched elements.
.value
No Such jQuery Method Exists
Just as an alternative to the already provided solutions... you could also use a Regex to test that it actually contains something other than whitespace.
!!$('#StartingPrice').val().match(/\S/)
This will test for the existing of a non-whitespace character and using the Not-Not will convert it to a Boolean value for you. True if it contains non-whitespace. False if blank or only whitespace.
if ($('#StartingPrice').val() === ""){
alert("ERROR!");
}
if ($('#StartingPrice').val() == ""){
alert("ERROR!");
}
If the value of your text input is any empty string then it's empty.
I think you want this:
if ($('#StartingPrice').val() == false) {
alert("Error!");
}
Use the .val() method to get the value and then pass that into the if. If the string is empty or white space it will evaluate to false.
Try:
if(myStr){
// Your code here
}
本文标签: jqueryHow do I test for a empty stringnullusing JavaScriptStack Overflow
版权声明:本文标题:jquery - How do I test for a empty stringnull, using JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738524255a2092349.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论