7

I'm look for an elegant way to check if input value is not empty and not default.

For example let's say we have input with value Username, this is a default value, and once the user clicks on this input field Username would be deleted so user could enter what he wants. Just to make it clear this is how it would look when the page is loaded:

<input type="text" class="defaultValue" value="Username" />

Now by default this field would never be empty, unless user would do something to make it empty, so just to make sure i need to check for both things. The way i would do this is very simple:

if($(".defaultValue").val().length == 0 || $(".defaultValue").val() == "Username")
   alert("Please enter your username");

So this works, but looks ugly as hell. And usually i need to deal with huge forms so my code gets really ugly and hard to manage because of this, any way to improve this?

Thanks.

5
  • 7
    Why don't you use placeholder attribute? Commented Mar 6, 2013 at 22:16
  • @undefined I have never heard about that attribute. Commented Mar 6, 2013 at 22:17
  • @undefined Just took a look at documentation and it says Note: The placeholder attribute of the input tag is not supported in Internet Explorer 9 and earlier versions. And i need to support IE 9. Commented Mar 6, 2013 at 22:18
  • @undefined—placeholders aren't supported by all browsers. But yes, that's a better solution. Commented Mar 6, 2013 at 22:18
  • There are many plugins for placeholder attribute, one of them is this github.com/parndt/jquery-html5-placeholder-shim, it's only 3kb. Commented Mar 6, 2013 at 22:20

2 Answers 2

18

Try:

if (this.value == '' || this.value == this.defaultValue) {
  // value is empty or is default value
}
Sign up to request clarification or add additional context in comments.

5 Comments

So what is difference ? only removing 0 can make it pretty ?
Well this definitely looks better.
@Garray—removing the unnecessary jQuery cruft and using direct property access means it's not only way less code, but probably about 20 times faster.
@ RobG Well now i am agree with you as there was no reference in Answer that it makes code fast i raised the comment
I'm accepting this answer even thou i chose to use undefined provided jQuery plugin, but this is also a very good solution.
4

Try writing a helper function to do this check for you.

var isEmptyOrDefault = function(field, default) {
    var value = $(field).val();
    return (value.length === 0 || value === default);
};

So you can use it:

if (isEmptyOrDefault($('.defaultValue'), 'Username')) {
    alert("Please enter your username");
}

3 Comments

In your example, you pass the field argument as a jQuery object, but then inside isEmptyOrDefault() you have $(field).val() which would result in $($('.defaultValue')).val() which is not going to work.
@undefined huh, so it does. Well, sweetamylase is lucky that jQuery is so forgiving :)
@idrumgood jQuery will always check if the variable passed to it is an instance of jQuery. The example is just for demonstrating that introducing a helper function could keep the rest of the code logic more manageable.

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.