0

So I've inherited this form with prefilled form fields like "First Name", "Phone" or "Zip Code" so users will know what to do with the fields. But if anyone just hits the button, the form is passing "First Name", "Phone" or "Zip Code" into the database. The onFocus in the doesn't appear to be working. Any ideas?

<input type="" maxlength="25" name="firstname" tabindex="6" id="firstname" class="textfield" value="First Name" onFocus="if (this.value == 'First Name') {this.value = '';}">

Thanks in advance, -Bob

3
  • 1
    Try using placeholder="First Name" if you are using an HTML5 supported browser Commented Feb 17, 2012 at 2:01
  • 1
    To get the most out of Stack Overflow, you need to put in some effort. Your questions need to give enough context and detail to allow folks (like myself!) who'd like to help a chance to do so. In this case, please: show a snippet of the form HTML, show any Javascript that is involved, and detail the exact expected/desired behavior. Thanks! Commented Feb 17, 2012 at 2:02
  • <input type="" maxlength="25" name="lastname" tabindex="7" id="lastname" class="textfield" value="Last Name" onFocus="if (this.value == 'Last Name') {this.value = '';}"> Commented Feb 17, 2012 at 2:13

3 Answers 3

1

First off it's bad practice, labels should be labels, placed before the field they are labelling. Not inside.

Name: [________]
=== NOT ===
[Name____]

Just like a form on paper.

If you must have the text in the box, use the placeholder attribute. But be aware that only the newest browsers support it.

As for solving the problem of default values being submitted, just check if the value you're about to enter in the database is the default value and, if so, block it.

Sign up to request clarification or add additional context in comments.

2 Comments

What's so bad about it? There are situations where one might be preferable over the other.
Once the user has replaced the default with a value, they no longer have a label telling them what the field is for.
0

As others have suggested, use the placeholder attribute to set the 'label' of the field. In addition, if you are already using jQuery to simplify your Javascript, you can use a plugin like this one which will add support for the placeholder attribute for non-HTML compliant browsers.

These plugins typically also take care of the crucial step of removing the placeholder text from your fields just prior to the form being submitted to the server, which should send blank values to the server, rather than the placeholder text.

1 Comment

Also, if accessibility is a concern at all, it would be good if you could include <label> elements as well, but then hide them with CSS.
0

In addition to what Kolnik said, you can do a simple validation for text inputs that have a default value using something like:

if ( input.value == input.defaultValue || /^\s*$/.test(input.value) ) {
    // field value isn't valid
}

to check if the value is still the default, empty or whitespace. But remember that client–side validation is completely unreliable so do the real validation on the server.

3 Comments

I believe you mean /^\s*$/.test(input.value)
Ah yes, thanks. Unlikely that the value will be a RegExp. :-)
Thanks everyone for the 'placeholder' tip. That's just what the doctor ordered. Just one last thing...what can I do about IE 7 or 8? (I'm afraid I know the answer;-)

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.