Text masking is an alternative for us to display information what type of content user must input to our forms, as usual jQuery make our life easier, to implement that we just need to create the script that read user actions (focusing or bluring the input), read its value then decide to empty the input value or refill with its default value.
$('input').focus(function() { if($(this).val() == "Enter your email here") $(this).val(''); }).blur(function() { if($(this).val() == "") $(this).val('Enter your email here'); });
To plain? Don’t worry we can manipulate them with animation, so if the user give a focus on the input we’ll fading out the text mask and vice versa. To implement that, we must add a label element before the input element and make its position to absolute. Take a look at following script :
HTML
<form> <label class="username-label" for="username">Username</label> <input type="text" name="username" class="username" /> <label class="password-label" for="password">Password</label> <input type="password" name="password" class="password" /> </form>
CSS
.username-label, .password-label { position: absolute; margin: 9px 9px 9px 12px; }
JavaScript
$('.username-label, .password-label').animate({ opacity: "0.4" }) .click(function() { var thisFor = $(this).attr('for'); $('.'+thisFor).focus(); }); $('.username').focus(function() { $('.username-label').animate({ opacity: "0" }, "fast"); if($(this).val() == "username") $(this).val() == ""; }).blur(function() { if($(this).val() == "") { $(this).val() == "username"; $('.username-label').animate({ opacity: "0.4" }, "fast"); } }); $('.password').focus(function() { $('.password-label').animate({ opacity: "0" }, "fast"); if($(this).val() == "password") { $(this).val() == ""; } }).blur(function() { if($(this).val() == "") { $(this).val() == "password"; $('.password-label').animate({ opacity: "0.4" }, "fast"); } });
If we can use fade in or fade out animation, then we can play with another animation such as slide to the left or right, once again it’s based on our need. Ok, that’s it, if you’ve another style for input masking you can share them in the shout box bellow, thank you for reading ;).
You can get regular useful updates about web-stuff by subscribing webstuffshare RSS feed or webstuffshare FREE Email subscription. If you like this post, your sharing and feedback would be very appreciated
Mask Your Input Forms and Make It Beauty (via @hdytsgt)
[Community News] jQuery Fundamentals
RGraph : HTML5 Canvas Graph Library
jQuery Plugin : jQuery iPhone UI
Pikachoose : Another jQuery Plugin for Image Gallery
HTML5 Security Cheatsheet
[Community News] How to turn any jQuery plugin into a Wordpress one
Zoom Your Element Using Zoomooz.js
[Community News] Build a Flash-like Game With Scripty2
[Community News] CSS3 Transitions available on Firefox 3.7
[Community News] CSS from the Ground Up
- How To Create iPhone-Style Navigation (Part I)
This post has got 23 shouts so far - jQuery Plugin : jConfirmAction
This post has got 33 shouts so far - iPhone-Style Navigation:AJAX + Rotate (Part II)
This post has got 12 shouts so far - Howto Create Mac-like Login Form
This post has got 7 shouts so far - Simple Flip Puzzle Effect with jQuery
This post has got 6 shouts so far - Implement Password Strength Meter & Gauge.js
This post has got 8 shouts so far - Create Animated Navigation Menu From Stratch
This post has got 7 shouts so far - Stylize Your Own Checkboxes
This post has got 15 shouts so far
- nice tut but I am newbie and I wonder how to make more one or two drop-menu
by b52 on Simple CSS3 Dropdown Menu - @andrew : Until now jConfirmAction still in development for...
by Hidayat Sagita on jQuery Plugin : jConfirmAction - sorry i must correct my prev post the links are without href blah blah
by andrew on jQuery Plugin : jConfirmAction - thanks! CSS 3 rulit))
by web desing on Photoshop Effect vs CSS3 Properties - Good!Thanks for share with the resource!
by ChunPIG on Free Web UI Element Pack




uberVU - social comments
Social comments and analytics for this post…
This post was mentioned on Twitter by developerupdate: Mask Your Input Forms and Make It Beauty http://is.gd/abT6F #jQuery…
CSS Brigit | Mask Your Input Forms and Make It Beauty
Mask Your Input Forms and Make It Beauty…
Text masking is an alternative for us to display information what type of content user must input to our forms, as usual jQuery make our life easier….
Kawsar Ali
Your site is really great. Just discovered. Been reading up all the post. Good Luck.
Hidayat Sagita
@Kawsar Ali : It’s a pleasure to hear that and knowing my posts are usefull, thank you!
Replace Flash with jQuery:Tutorials and Resources
[...] Mask Your Input Forms and Make It Beauty [...]
wespai
Good Job
collect it to http://ajax.wespai.com
Omoba
I love it, I have been seeing and trying to achieve the like of this, not until I saw it on your blog the goose chase would still be on.
Can I use the source code for my project? Please reply my question using the email I droped above.
Thanks.
Jim
Are the lables hidden when there INPUT field had a value?
Hidayat Sagita
@Jim : Yes of course, except for the example #3 (Sliding), the labels will positioned on the right side of the input.
Jim
@Hidayat : I tried it with example 3 but it didn’t work.
Hidayat Sagita
@Jim : What’s the problem, can you put the link or problem description?
Jim
Hi Hidayat,
I can’t give you a like as it’s on an intranet. However, you can seen an example here:
http://i45.tinypic.com/27y6c7d.png
The Grey text that reads “Label Name” is the label, and the “xxx” text is the value of the input field.
Jim
Where in your code does it actually detect the existence of a value in the input field that is pre-populated from the server?
Hidayat Sagita
@Jim : I see, if the value has exists (pre-populated from the server for example) you can make it auto focus :
if($(’.username-sliding’).val() != “”) {
$(’.username-sliding’).focus();
}