|
I have the following code:
<script type="text/javascript">
function myCalc()
{
var t00 = document.getElementById("YesNo");
var t01 = document.getElementById("txt01");
var t02 = document.getElementById("txt02");
var t03 = document.getElementById("txt03");
var t04 = document.getElementById("txt04");
if (t00.value == "1")
{
t03.value = t01.value - t02.value;
t04.value = t01.value - t02.value;
}
else
{
t03.value = (t01.value - t02.value) * 0.85;
t04.value = t01.value - t02.value;
}
}
</script>
Currently if alphanumeric characters are entered the calculation displays a NaN. In the event alpa numberic characters are entered I would like it to display "0" instead of NaN. Is that possible?
|
|
|
Sure, use the "isNaN()" function. So test your result, if isNaN returns true, then assign "0" to your value.
|
|
|
|
|
I'm sorry. I'm new to javascript. ASP is my forte. Could you possibly give an example?
|
|
|
<html>
<head>
<script type="text/javascript">
function checkN(x)
{
if (isNaN(x.value) == true)
{
x.value = 0;
}
}
</script>
</head>
<body>
<form>
<input type="text" onblur="checkN(this);"/>
</form>
</body>
</html>
|
|
|
|
|
Sorry...I guess I need to be a little more descriptive. Here is my current script:
<script type="text/javascript">
function myCalc()
{
var t00 = document.getElementById("YesNo");
var t01 = document.getElementById("txt01");
var t02 = document.getElementById("txt02");
var t03 = document.getElementById("txt03");
var t04 = document.getElementById("txt04");
if (t00.value == "1")
{
t03.value = t01.value - t02.value;
t04.value = t01.value - t02.value;
}
else
{
t03.value = (t01.value - t02.value) * 0.85;
t04.value = t01.value - t02.value;
}
}
</script>
I want it to where if a user enters an alphanumeric character into txt01 and txt02 then txt03 and txt04 have a value of 0.
|
|
|
Hey, this script looks familiar! Try this version:
<script type="text/javascript">
function myCalc()
{
var t00 = document.getElementById("YesNo");
var t01 = document.getElementById("txt01");
var t02 = document.getElementById("txt02");
var t03 = document.getElementById("txt03");
var t04 = document.getElementById("txt04");
if ( (isNaN(t01.value) == true) && (isNaN(t02.value) == true))
{
t03.value = 0;
t04.value = 0;
}
if (t00.value == "1")
{
t03.value = t01.value - t02.value;
t04.value = t01.value - t02.value;
}
else
{
t03.value = (t01.value - t02.value) * 0.85;
t04.value = t01.value - t02.value;
}
}
</script>
That will check to see if BOTH t01 and t02 are alphanumeric. If you want to have 3 & 4 to be zero if EITHER 1 or 2 is alpha, change the "&&" to "||".
In English: if the value of t01 AND the value of t02 are both NOT Numeric, then make the value of t03 and t04 zero.
You know the score: if this post was helpful, click Ads! My site is also advertiser supported (hint, hint).
|
|
|
|
|
Hmm....
It is still displaying NaN. Click on the following link http://63.174.89.102/QNI/ReconChange/sales_form.asp. There are two form fields, "Change From" and "Change To". Currently, when you put numberic characters in those fields and hit the "Calculate" button a calculation is performed and the results are displayed in the net and gross adjustment fields. Try typing alphanumeric characters in the Change From and Change To fields...then press Calculate. It will display NaN...I want it to display 0.
|
|
|
You have multiple script blocks with the myCalc() function. Make sure you edit out the "old" one.
To make my addition work, add "return":
if ( (isNaN(t01.value) == true) || (isNaN(t02.value) == true))
{
t03.value = 0;
t04.value = 0;
return;
}
This will cause the script to abort immediately, setting the values of txt03 and txt04 to 0.
If you don't return, then the script runs and re-calculates t03 and t04.
|
|
|
|
|
PERFECT!!!!!
THANK YOU SO MUCH. Is there anything I can do for you?
|
|
|
I went to your website and clicked on about every link I could find. I also see where you are a guitar player. Me too!!
www.cjandthebigbrotherband.com
|
|
|
Thanks for clicking the links... of course, it's the ads that pay!
I know it's cheesy to keep mentioning ads, but sites like Codetoad and my own survive on ad revenue.
I'm happy to help! I couldn't get your audio bytes to play, what gives?
|
|
|
|
|
Ya just need to be patient. When you click on it you will see that its downloading in the bottom of your browser. They are rather large MP3s so if your on a dial up line...well you'll be waiting for awhile. Otherwise just be patient.
Thanks again.
|
|
|
Sounds good! I don't play in a band, but my family is very musical. Acoustic and Electric guitar, mandolin, piano... a good friend of mine and co-worked plays bass and has been in bands for years.
|
|
|
|
|
I too come from a very musical family. As you can see from the website, I play with my two older brothers. Our father toured for awhile with a country western band in the 80s. We have an uncle who is fluent on about any stringed instrument made; mandolin, guitar, banjo, etc.
|
|
|
Greetings...
I was hoping that you might be able to further help me on a javascript that you wrote for me. To refresh your memory, please refer to the following code:
<script type="text/javascript">
function myCalc()
{
var t00 = document.getElementById("YesNo");
var t01 = document.getElementById("txt01");
var t02 = document.getElementById("txt02");
var t03 = document.getElementById("txt03");
var t04 = document.getElementById("txt04");
if ( (isNaN(t01.value) == true) || (isNaN(t02.value) == true))
{
t03.value = 0;
t04.value = 0;
return
}
if (t00.value == "1")
{
t03.value = t01.value - t02.value;
t04.value = t01.value - t02.value;
}
else
{
t03.value = (t01.value - t02.value) * 0.85;
t04.value = t01.value - t02.value;
}
}
</script>
<script type="text/javascript">
function direct()
{
var t00 = document.getElementById("YesNo");
var t05 = document.getElementById("txt05");
if (t00.value == "1")
{
t05.value = "DIRECT";
}
}
</script>
This script works great for me. The only other thing I need is to reflect that these fields will be displaying $ therefore I need everything to be formatted accordingly. ex. $456.45
Make sense?
|
|
This 18 message thread spans 2 pages: [1] 2 > > |