If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Why won't this script accept decimal point?

This is my first attempt at a script. I want it to accept a number with decimal point, and it will not. I altered it already once and it accepted decimal points, but then it stopped calculating the result. Var 2 and Var 3 are the only fields that need to have decimal points possible. Also, is it possible to recode this so var 3 can be entered as a whole number? I want the person to enter a profit percentage, like 30 for 30 percent, but I could only figure it out by making it 0.3 Any help would be greatly appreciated.

If you are trying to perform calculations on numbers entered into fields by the user, then you have a great many problems, incluing validation of the inputs to ensure that they are numbers (within a sensible range). It is pointless to use onkeyup here. The calculation will only make sense if all four numbers have been entered. But you must rename or (re-id) your form fields so as to not duplicate the Javascript variables.

Be aware that form field values are strings unless/until converted to numbers by one of several methods, Number() being recommended.

Code:

var val1= Number(document.getElementById("v1").value) || 0; // || 0 traps NaN entries and changes them to 0.
val1 = Math.floor(val1); // (or Math.round) - make val1 integer
// val1 is the name of the Javascript variable. v1 is the id of the form field.
// Now check that val1 is within a sensible range, and (presumably) not negative.

Finally, put newlines between each line of your HTML code. It makes it unreadable if you run them all together into one long line.

onkeyup="javascript: setValue(this)"javascript is completely unnecessary - I have forgotten exactly why in the distant past it was required in some early browsers.

Your style sheet is also purposeless. It specifies what are the default colors anyway. I would advise you not to try to bite off too much at one sitting - keep your early attempts simple and check them frequently to see if they run OK, then build on your work. Rather than Coffee Cup Editor I would recommend that you use plain old Notetab or Notepad++.

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

Last edited by Philip M; 02-13-2013 at 06:59 PM.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Thank you for the help

As I mentioned, this is my first attempt at javascript, and I took the code from a sample. The reason if filled in the variables at the beginning was to serve as a sample set of values, which I wanted the user to adjust.

Your explanation went over my head, as I am so new to javascript. Could you fix the code I submitted so it works properly and allows people to enter a decimal point on field 2 and a straight percentage value (like 30)?

Your explanation makes sense, but modifying the code with your fixes is beyond me, it is so easy to make mistakes with this.

I would be extrememly grateful if you could show me the whole solution.

AlanP - You should be aware that it is not really in your best interests that others do your all or most homework for you. Many people would regard that as cheating. Furthermore your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. This is especially so if "Your explanation went over my head". That means that you are very much out of your depth.

To repeat:- I would advise you not to try to bite off too much at one sitting - keep your early attempts simple and check them frequently to see if they run OK, then build on your work. Rather than Coffee Cup Editor I would recommend that you use plain old Notetab or Notepad++.

Hint: be aware that a large proportion of the code used with data which is entered by the user is validation of the inputs. Were it not for this the code would be little different from multiplying four fixed numbers. Again, be aware that form field values are strings unless/until converted to numbers by one of several methods, Number() being recommended. As it happens in this case (multiplication) automatic type conversion will apply - but do not rely on that!!! You still need to make the conversion for validation purposes.

If you want the user to enter a percentage value as 30 rather than .3, all you have to do is divide by 100 to get .3, add 1 to get 1.3 and then multiply the base value (say 10) by that (to give 13). But of course you must check that the value entered by the user is indeed a number such as 30 and not .3 as otherwise the answer is silly. So, for example, check that the percentage figure is either zero or greater than 1, and if not require it to be entered again. Zero is OK in the multiplication because 0/100 = 0 + 1 = 1.00.

To give you the flavour of how you could do this (but well beyond you at this stage):-

No, it's not homework. I'm trying to help out a guy I built a website theme for. I don't do javascript calculators, so when he wanted one, I gave it a shot by looking up on javascript source. The solution I came up with does work, but only if you don't try and alter the hourly rate field, or put in a decimal point. So, I am just out of my element here, and I'm trying to give him a solution that will work.

Ideally, it would be nice if the visitor was able to enter a percentage in the markup percentage field, and in the hourly rate, they could enter a number with decimal place, like 10.25 / hour 11.75, whatever.

Like I said, this isn't homework, I'm just out of my element doing javascript calculators.
Here is the page on the website I built for him where I put in the bad version I cam up with.http://ironmanconsultants.com/?page_id=412

I don't know how things work where you come from, but where I come from, it is considered rude to call someone a liar. I'm not in the habit of lying to people, nor was I in any way trying to pretend javascript was something I am well versed in.

Website design, graphics, page content and flow, and getting customers to buy whatever is is someone is trying to sell is what I do, and I do it quite well thank you very much.

What I don't do is insult perfect strangers who ask for help from me, especially when that person has not attempted to insult me.

Fair enough, if you say so. It seemed to me a very big contast between the expert coding of that page and the seemingly juvenile code offered in Post#1. It gave the impression of school homework to me (and to Old Pedant as well). Perhaps web site design competence does not include any knowledge of Javascript. I withdraw my comment.

Last edited by Philip M; 02-16-2013 at 05:27 PM.

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Perhaps web site design competence does not include any knowledge of Javascript.

It's not common in my experience to find web site designers without competence in JavaScript, but it does happen. The primary designer with the company I am primarily contracting to is a bit more competent than AlanP, from the evidence of this thread, but not very much more. If he didn't have tons of jQuery libraries to fall back on, he'd be pretty much lost. (The mildly funny part is that his assistant is much much more competent, but without the design skills, and the boss wants the designs done right, so...)

An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.