/    Sign up×
Community /Pin to ProfileBookmark

Javascript input masks

I need to create input mask to format date / ssn / currency. I see examples but wish to understand the / d* so I can figure out what Exactly wont my site to do.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Tweak4Jul 03.2007 — There are no input masks in JS. You can use regular expressions (the "/ d*" that you mentioned, though that is only part of one) in order to validate input, but the "masks" that exist in some other languages are absent in JS.

And for what it's worth, /d*/ would look for zero or more numeric characters ?
Copy linkTweet thisAlerts:
@nhnerdauthorJul 04.2007 — I need to process severial currency (money) feilds that reange from 1,000.00 to 100,000,000.00 and any where in between, when the user leaves the feild I wish to format any entry with the proper $,,,. format. I found JS to do the SS and phone numbers this way. but with the currency I noticed I have to loop through and detect the exact sets of (3). what happens now is if you place 25000 in it will change to $25,000. but if I tab back and out it will drop to 250. and so on. is there an easier why to do this then looping through 100,000,000. / 1,000,000. / 1,000. and so on,
Copy linkTweet thisAlerts:
@Tweak4Jul 09.2007 — If you post the function you are using to do this, we can probably help tweak it. On my company's site, we do something similar to this for formatting currency fields. In our usage, the first step of the format is to strip out any non-numeric fields- from there is is a simple matter to add commas where necessary.
Copy linkTweet thisAlerts:
@nhnerdauthorJul 09.2007 — function adjusttocurrency(t)

{

var completevalue = t.value;

var splitvalues = completevalue.split(".");

var dnumber = "."

// check for decimal points ans if greater then 1 point stop and turn default values
if (splitvalues[2] != null)
{
t.value = "To many . in " + t.value;
return;
}

// check valid range of deciaml number
var dnumberExp = new RegExp('([0-9]{2})');

if (splitvalues[1] > 0 && splitvalues[1] < 99) dnumber = dnumber + splitvalues[1];
if (splitvalues[1] == "0" || splitvalues[1] == "00" || splitvalues[1] == "" || splitvalues[1] == null) dnumber = dnumber + "00"
else dnumber = dnumber + splitvalues[1];

// add commas to the whole numbers
if (splitvalues[0] != null)
{
var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
while(objRegExp.test(splitvalues[0]))
splitvalues[0] = splitvalues[0].replace(objRegExp, '$1,$2');
t.value = splitvalues[0] + dnumber;
return;
}
}
×

Success!

Help @nhnerd 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...