Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3982902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:35:01+00:00 2026-05-20T05:35:01+00:00

So I have a form working quite well with MVC 3, DataAnnotations and Unobtrusive

  • 0

So I have a form working quite well with MVC 3, DataAnnotations and Unobtrusive javascript. However, I want to put a “watermark” on my input fields so that for example, the First Name textbox is populated with a value of “First Name” by default. When the user clicks on it, the value disappears and if they move off the field without entering anything, “First Name” appears again. Also, I have this implemented and working well.

My question has to do with the [Required] attribute on the FirstName property of my view model. If the user submits the form, by default that field has “First Name” in it so it passes the “Required” validation.

What’s the best way to deal with this…I’m thinking of a few options:

1) Inject some jQuery to fire before the unobtrusive JS validation that clears the watermarks so that when the validation fires, those fields that have default values in them are blank. I’m not sure this is possible. Can you somehow inject a function before unobtrusive JS runs its validation?

2) Modify the [Required] attribute or create a new one to accept a default value and then compare it to see if they match. This raises a few issues in that I now have my default value specified in multiple places, one in the UI and one in code and that feels wrong.

3) Create a new “Watermark” attribute that I decorate a property with that specifies the default value for that field. Create a new HTML helper (instead of TextBoxFor) that looks for this attribute and emits the appropriate attributes to the tag. Modify or create a new [Required] attribute that looks for the existence of [Watermark] on the same field. This keeps the “Default value” in one place and keeps with DRY principles, but it feels like I’m putting UI elements into code (watermarks are purely visual) and it also feels like an overly complex solution for what should be a simple issue.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:35:01+00:00Added an answer on May 20, 2026 at 5:35 am

    Each has it’s pros/cons. I like to keep this client side and have used the following.

    Use the HTML5 placeholder attribute and for browsers that support it you don’t have to do anymore.

    <input type="text" placeholder="First name" />
    

    For browsers that don’t…

    On every page we have some compatability script for browsers that don’t support certain features. In this case it’s a little bit of JavaScript and jQuery that detects if the browser supports the placeholder attribute. If it doesn’t it sets the field value to the placeholder value, sets the styling and adds the appropriate focus/blur event handlers. The focus/blur event handlers set the field value appropriately.

    Then on your client validation script check that field value doesn’t equal the placeholder value.

    In your case this would mean modifying your unobtrusive JS validation to check the field value doesn’t equal the placeholder value

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form with many input fields. When I catch the submit form
I have been working on quite a complex accordion/form recently and have a select
I'm working on a C# program, and right now I have one Form and
I have a WPF form and I am working with databinding. I get the
Form validation for check boxes only seems to be working in IE....? Anyone have
I have a form like this: <form name=mine> <input type=text name=one> <input type=text name=two>
I have several input and option elements on my page, each (well almost) have
i have been working on a new .net MVC site and have integrated some
I have an InfoPath 2010 form where I want a user to select an
I have quite a large IF statement which needs to update another form element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.