3

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

JaredMcAteer
22.7k5 gold badges52 silver badges68 bronze badges
asked Apr 21, 2011 at 15:56

7 Answers 7

8

try this:

if ($('#StartingPrice')[0].value.length == 0){
 alert("ERROR!");
}
answered Apr 21, 2011 at 15:58
Sign up to request clarification or add additional context in comments.

Comments

8

$('#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

answered Apr 21, 2011 at 15:58

6 Comments

10 seconds. You must have cheated.
@Raynos - Please there is no race here. We all here to help each other. There is no competition.
@Naveed thank you for standing up for me, some times users like Raynos can make people sad, but I try to stay positive, mostly through meditation and deep breathing.
@Hunter 8 answers? What's going on. @NaveedAhman there is a race. I have to beat Hunter!
@hunter I read it as intended to be quite a light-hearted comment
|
3

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.

answered Apr 21, 2011 at 16:04

2 Comments

you would still need to use val() not value()
Edited from value() to val().
2
if ($('#StartingPrice').val() === ""){
 alert("ERROR!");
}
answered Apr 21, 2011 at 16:00

Comments

1
if ($('#StartingPrice').val() == ""){
 alert("ERROR!");
}

If the value of your text input is any empty string then it's empty.

answered Apr 21, 2011 at 15:58

Comments

1

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.

answered Apr 21, 2011 at 16:01

1 Comment

Fixed, I was typing too fast!
-3

Try:

if(myStr){
 // Your code here
}
answered Apr 21, 2011 at 15:59

1 Comment

if there were a "Reject" option this would def win

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.