/    Sign up×
Community /Pin to ProfileBookmark

input mask — javascript

Does anyone know what javascript I would use to automatically format a field as the user types (or immediately after they type) to xx-xx-xxxx (a social security number)

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@russellAug 06.2004 — If you break it up into 3 fields, it gets a lot easier

<i>
</i>&lt;input type=text name=ssn1 onkeypress="if(this.length==3) ssn2.focus()" size=3 maxlength=3&gt;
&lt;input type=text name=ssn2 onkeypress="if(this.length==2) ssn3.focus()" size=2 maxlength=2&gt;
&lt;input type=text name=ssn3 size=4 maxlength=4&gt;


Then you can format it yourself. Something like:
<i>
</i>f = document.myForm;
ssn = f.ssn1.value + "-" + f.ssn2.value + "-" + f.ssn3.value;


Of course, this doesn't win any points for DOM compliance, but it ought to work in most browsers...
Copy linkTweet thisAlerts:
@steelersfan88Aug 06.2004 — This will work in IE and Mozilla, despite needing to change the Perl script that will retrieve the values:[code=php]<form name="myForm" action="script.pl" method="post">
<input type="text" name="ssn1" hidVal="" onkeyup="if(value.length==2){ssn2.focus();mask(this)}" size=2 maxlength=2>
<input type="text" name="ssn2" hidVal="" onkeyup="if(value.length==2){ssn3.focus();mask(this)}" size=2 maxlength=2>
<input type="text" name="ssn3" hidVal="" onkeyup="if(value.length==3){mask(this)}" size=4 maxlength=3>
</form>

<script type="text/javascript">

function mask(obj) {
var len = obj.value.length;
for(var i=0,a='';i<len;i++) {
a += "x";
}
obj.setAttribute('hidVal',obj.value);
obj.value = a;
}

</script>[/code]
The real values are stored in hidVal, while value becomes x's. Remember that these fields can still be edited.

Social secuties are not always in the format xx-xx-xxx, they are quite often oter combos of up to 11 digits in 3 sets, either 2,3, or 4 a set.

Dr. Script
Copy linkTweet thisAlerts:
@jarryOct 04.2006 — Check this web site [URL=http://www.wiseblocks.com]www.wiseblocks.com[/URL]
Copy linkTweet thisAlerts:
@chuckyargSep 06.2007 — Does anyone know what javascript I would use to automatically format a field as the user types (or immediately after they type) to xx-xx-xxxx (a social security number)[/QUOTE]

take a look at this site

http://jazz-solutions.com.ar

Cheers
×

Success!

Help @esthera spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...