/    Sign up×
Community /Pin to ProfileBookmark

How to format data of input box

Dear Frnds,

I want to make format for Input box like predifined format as for example if i tell “0.000” input shuld become 10.000 or date format or currency format when i got out of input box it shd be come like that format or if data is already there than it should get that format.

Can we make that format global and set its attributes.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisDec 28.2006 — Yes, that could be done. But to make that global and automatic is a pretty large task -- not one which most visitors to this site would be willing to tackle for you and for free. (Note that only site visitors answer questions here -- there are no paid monitors to assist.)

It is generally easier (quicker) to just do what you need and at the time you need it. For example, the .toFixed() method will insure that you get the number of desired decimal places. This is so that if the user inputs "10" and you want two decimal palces (like for normal currency) then you can do something like this:

var input = "10"; // this would come, e.g., from a form field

var num = Number(input);

if (isNaN(num)) num = 0;

var output = num.toFixed(2);

alert(output);
Copy linkTweet thisAlerts:
@milindsaraswalaauthorDec 29.2006 — What abt if i want to put currency fomat my customize format or date format like dd-mm-yyyy

How it can be acchived
Copy linkTweet thisAlerts:
@so_is_thisDec 29.2006 — To format the current date as you've outlined, you can do this:

var x, curDate = new Date();

var dd = ((x=curDate.getDate())<10)?'0'+x:x;

var mm = ((x=curDate.getMonth()+1)<10)?'0'+x:x;

var usrDate = dd + '-' + mm + '-' curDate.getFullYear();

alert(usrDate);
Copy linkTweet thisAlerts:
@milindsaraswalaauthorDec 29.2006 — thanx so_is_this for reply. I have small problem. Plz if u can help in that. I m using following code for changing color of background and color of font when it is focus and become as it is when focus come out.
[Code]
function setInputEvents()
{
var myInputCollection = document.getElementsByTagName("input");
var mySelectCollection = document.getElementsByTagName("select");
for (var inpIdx=0;inpIdx<myInputCollection.length;inpIdx++)
{
myInputCollection[inpIdx].onblur = function(event){inpAction(event)};
myInputCollection[inpIdx].onfocus = function(event){inpAction(event)};
}
for (var inpIdx=0;inpIdx<mySelectCollection.length;inpIdx++)
{
mySelectCollection[inpIdx].onblur = function(event){inpAction(event)};
mySelectCollection[inpIdx].onfocus = function(event){inpAction(event)};
}
}

function inpAction(e)
{
var evt = e||event;
if (!evt.srcElement) evt.srcElement = evt.target;
evt.srcElement.className = evt.type.match(/focus/)?evt.srcElement.inpType||evt.srcElement.getAttribute("inpType"):"";
}

function setInputType(inpID, inpType, inpFormat)
{
var inpElement = document.getElementById(inpID);
if (!inpElement) return true;
inpElement.setAttribute("inpType",inpType);
}
[/Code]


I want to integrate ur code in inpFormat For example i put number format i.e 0.000 or 0.000 KD

I m puting above code in Onload like this
[Code]
<script language="javascript" type="text/javascript">
onload = function() {
setInputEvents();
setInputType("fldEnFirstName", "InputStyle", null);
}

</script>
[/Code]
×

Success!

Help @milindsaraswala 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,
)...