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 6247307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:50:45+00:00 2026-05-24T12:50:45+00:00

I want to use jQuery Masked Input Plugin (http://digitalbush.com/projects/masked-input-plugin/) in my registration form. My

  • 0

I want to use jQuery Masked Input Plugin (http://digitalbush.com/projects/masked-input-plugin/) in my registration form. My form looks like that

…

<input  class="part1" type="text" name="lname"  value="Last Name" onfocus="if(this.value=='Last name') {this.style.color='#aea592';this.style.fontStyle='normal'; this.value=''};" onblur="if(this.value=='')  {this.style.color='#aea592'; this.style.fontStyle='italic'; this.value='Last name'}"  />
<input class="part2" type="text" id="dob" name="dob"  value="Date of Birth" onfocus="if(this.value=='Date of Birth') {this.style.color='#aea592';this.style.fontStyle='normal'; this.value=''};" onblur="if(this.value=='')  {this.style.color='#aea592'; this.style.fontStyle='italic'; this.value='Date of Birth'}" />

…

a the end of the file

<script src="core/code/js/mask.js"></script>
<script type="text/javascript">
jQuery(function($) {
    $('#dob').mask('99.99.9999', {placeholder:' '});
});
</script>

When you open the page, Last Name field shows word “Last name” but date of birth doesn’t show anything. http://prntscr.com/2miaa How can i activate mask plugin for only onfocus event?

  • 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-24T12:50:47+00:00Added an answer on May 24, 2026 at 12:50 pm

    Yarr… Ok, first don’t put so much script inside your markup. It’s difficult to read, and it’s going to be even more difficult to maintain. Lets try separating the style and functionality of your form first.

    Edit: A working example of this can be found at http://jsfiddle.net/ninjascript/RuFHK/2/

    CSS

    input { color: #aea592; }
    input.default { font-style: italic; }
    

    HTML

    <input class="part1 default" type="text" name="lname" value="Last Name"/>
    <input class="part2 default" type="text" id="dob" name="dob" value="Date of Birth"/>
    

    JavaScript

    $('input').bind('focus', function() {
        var el = $(this);
        if (el.hasClass('default')) {
            el.removeClass('default').val('');
        }
    });
    

    phew… OK that’s a bit easier to understand. Now you want just one more thing to happen on focus, and that’s to apply a mask to the value contained in the #dob element. Here’s the edited block:

    $('input').bind('focus', function() {
        var el = $(this);
        if (el.hasClass('default')) {
            el.removeClass('default').val('');
        }
        if (el.attr('id') === 'dob') {
            $(this).mask('99.99.9999', {placeholder:' '});
        }
    });
    

    Now your input should only be masked after it gains focus.

    If you want to make sure that some value is returned on submit, you can always validate the content on ‘blur’. I don’t really feel like writing if statements for every field in this form, so I’m just going to map each field name and it’s default value and refer to that:

    var defaultValues = {
        'lname': 'Last Name',
        'dob': 'Date of Birth'
    };
    

    Now I can just refer to that map whenever I need to repopulate the field with my default values. In the case of the DOB field, it looks like the input mask plugin also has an ‘unmask’ option. Unfortunately because the characters within the mask are part of the #dob field’s value, we can’t just check for an empty value. Instead I’m going to use a regular expression that checks for a value that consists of nothing but whitespace and ‘.’ chars:

    $('input').bind('blur', function() {
        var el = $(this);
        var name = el.attr('name');
    
        // Really we only want to do anything if the field is *empty*
        if (el.val().match(/^[\s\.]*$/)) {
    
            // To get our default style back, we'll re-add the classname
            el.addClass('default');
    
            // Unmask the 'dob' field
            if (name === 'dob') {
                el.unmask();
            }
    
            // And finally repopulate the field with its default value
            el.val(defaultValues[name]);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use jQuery to submit my form and i can do that
I want to use jQuery.contextMenu: http://abeautifulsite.net/blog/2008/09/jquery-context-menu-plugin In jQuery.fullcalendar when I right-click on an event
I want to use Jquery to implement a page that contains js from quotemedia.com.
I have a large form (30 different input fields) and want to use jQuery's
I want to use jquery Chili plugin for syntax highlighting a piece of code
I don't want to use jQuery, but I'd like to use Ajax to do
I want to use the jQuery accordion tool for a form i'm building so
i want to use jquery ui auto complete plugin in asp.net. this is my
hos can create masked Input for date and time by jQuery? i not want
I want to use jQuery in an add on JS library that can be

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.