how to use JavaScript to calculate the currency - javascript

hi I actually trying to do calculate the two numbers,, it was working, but now I don't know how to turn the number into currency. so before the number it need to show £ sign.
I was researched in Google ,and i tried some things but its not actually working.
var name,child,result;
function setvalues()
{
name=Number(document.getElementById("name").value);
child=Number(document.getElementById("child").value);
}
on that name field user has to enter some number to calculate,, its was calculating but just a number not with an currency sign... I seen some of the JavaScript function to change the currency format but its not very clear... and how to add three vaules together and show in one textbox with £ sign

I am unsure of what you are trying to accomplish but I will try to answer as best I can. I would recommend that if you are working with currency(which can be a especially be a pain in JavaScript). I would recommend using a library to handle this for you. I have used accounting.js in the past and it works very well for these sorts of things:
http://josscrowcroft.github.io/accounting.js/
If you update your question with more information I will update my answer.
UPDATE
Try this: http://codepen.io/anon/pen/BjIHn/. It should produces the output you are wanting.

Related

onChange handler that formats numbers with commas and allows up to two decimal places

I am struggling to figure out the proper onChange handler that will allow me to format a number input with commas and also allow up to two decimal places.
Test Cases:
input: '1234.56' -> output: '1,234.56'
input 'aaaa1234567.34' -> output: '1,234,567.34'
I do not want this formatting to occur onBlur, I want the formatting to occur onChange so the commas are inserted as you type.
This is my first stack-overflow question so forgive me if my write up is not great.
I have searched for working examples on stack-overflow. The closest thing I found was this, but it does not work in Safari (I followed the comments but could not find a solution). I am not great at regex and have spent about 5 hours trying to figure this out.

How to validate date on the client side in jsp form without javascript?

The date picker doesn't show up even though i use
<input type="date" name="dob" required>
Additional information that might help you help me :
It is in a JSP file
I'm using tomcat v7.0
I need to validate the date without using JavaScript?
I tried using text for input and setting a specified format but then how can I add range there? (min max and dynamic for February issues?)
Can this issue be addressed by using combobox? How can I change value options of one combo box depending on the other? (Date options dependent on month and/or year)
Any help is highly appreciated.
P.S. Since it is a part of a registration form, I have been advised against using JavaScript validation (as it can be disabled)
Edit : I guess I'll go with a regular expression. I found one here, the second answer to this question : Regular Expression to match valid dates
The date picker doesn't show up
It does when I test it. Perhaps you are using a browser which does not support it.
I tried using text for input and setting a specified format but then how can I add range there?
If by "format" you mean a pattern, then you would need a complex regular expression that had multiple parts.
e.g. 0 followed by 1-9, or 1 followed by 0-9, or 2 followed by 0-8 followed by / followed by 02 followed by (leap year logic).
It wouldn't be short or pretty.
Regular expressions do not lend themselves to describing the format of dates.
That said, see this question.
Can this issue be addressed by using combobox?
You can't have a combobox in HTML without using JavaScript, which you ruled out.
If you mean "a collection of select elements" then that would be "dropdown menus" not "a combobox".
You could use those, but there's no good way to stop people entering dates like the 31st of February.
How can I change value options of one combo box depending on the other?
Only with JavaScript, which you ruled out.
Since it is a part of a registration form, I have been advised against using JavaScript validation (as it can be disabled)
You shouldn't depend on JavaScript for input validation because it can be bypassed… but that is true of any client-side input validation you might implement.
Client-side input checking is useful because it can give users rapid feedback if they make a mistake and enter data which doesn't make sense.
You need to accompany it with server-side input checking in order to prevent bad data being inserted into your system deliberately.

Generating any # of random math questions based on user input in Javascript using LOOPS

My task is to create a program in Javascript that generates however many math questions the user wants based on their answer through a prompt. So if the user were to answer the prompt saying they want 5 questions, I would need 5 math questions generated, all with random numbers that change every time the page is refreshed. (questions can be very simple math problems)
The program also needs to ask the user for the answers to the questions, and display their answer AND the correct answer to the questions on the webpage afterwards. Loops also need to be involved in the code for this exercise.
I am a beginners level web programming student, and I am not too sure where to start to code this, because my teacher has not given us many examples and practice for using loops to make this programs. Any suggestions?
You have to use Javascript? No other langaue possible? Javascript won't be the easiest language for a beginner...
But if you have to use it:
- Generate a HTML Unput field where User can enter a Number of how many math questions he want
Evaluate the number and run a loop, wich generates the numbers of question.
Use a Random Method for generating numbers.
Take look at this: https://gist.github.com/kerimdzhanov/7529623
Display the questions with a html input field for the answer of the user
Solve the question with javascript
Compare the answer of the user with your result
Display if true or false, display your calculated result for showing the correct answer.

knockoutjs format numbers with commas

I'm new to knockout and just starting to get my head around the framework. However, I've come into a problem whereby I'm trying to format large numbers with commas. I've been able to get the number to format to decimal places using the extenders API but this isn't what I want.
The number is stored in an array and an example of a number used in the app will be 5 million. So I need the values to print out 5,000,000 - is this possible? I'm guessing it has to be.
For the formatting, you can use the following regex (warning: it does not work with float): mystring.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
You can check out this example: http://jsfiddle.net/nyothecat/XgezN/1/
I think you'll want to create a custom binding. The easiest thing would probably be to use an existing jQuery formatter to format the display when the observable changes and then set up an event handler for the textbox to parse the textbox (removing the commas) when the users types something new.

Style strings separated by spaces

I have a form where I can edit event information for a calendar. These specific events run every single week, but one of the form fields is for "days off" where you can enter days where the event doesn't run. using mysql and php, any current days off will be displayed in a textarea field, separated by spaces. You will also be able to put in more days off by typing in a date and making spaces. I would like to be able to style each date just like right here on stack overflow when you ask a new question and put in the tags. After you type in a tag, it gets surrounded by a box, turns blue, and gets an x for deleting it. This is what I want.
Here's a link to a screenshot of what I want it to look like.
http://www.uvm.edu/~sass/screenshot.jpg
I'm not sure how it works with dates and formatting them, but as for displaying in such style I've been using Select2 component. If you can add parsing or create valid list of dates it should give you the correct result.
Okay, I have found a solution by digging around the internet.
Stack Overflow style tagging system in jquery (view the second answer)
Someone created exactly what I want. The results are stored in a hidden field as a comma separated list so I can easily explode it into an array for use with my database, and it formats the inputs nicely with css. I modified the look with css and changed the accepted formats to work with dates and here is the result.
http://www.uvm.edu/~sass/screenshot.png

Categories