Or simply:
function calc(thevalue){
thevalue = thevalue * 1;
if (isNaN(thevalue)) {
thevalue = 0;
alert(thevalue);
}
}
Main Topics
Browse All TopicsI'm trying to calculate some fields using an onchange event. If a field is blank, I want javascript to interpret it as a zero to use in a calculation, but I can't figure out what I'm doing wrong. Here is my function:
function calc(thevalue){
if (thevalue = "" || isNull(thevalue)) {
thevalue == 0
alert(thevalue)
}
}
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I have a follow up queston and I'm glad to add more points to this question to get an answer...based on the answer given above, I have the following two functions:
I only want the calculate functon to execute if the checkit function is false, otherwise, I want the functions to stop executing and the focus to return back to the field that is not valid. Does that make sense?
The line that I'm calling these funcitons looks like this:
onchange="checkit(this.val
Thanks.
function checkit(thevalue){
if (thevalue > 5 || thevalue == 0) {
alert("The value you have entered is not valid")
}
}
function calculate(prefix, thenumber){
field1 = document.getElementById(pr
if (field1 == "" || isNaN(field1)) {
field1 = 0
}
field2 = document.getElementById(pr
if (field2 == "" || isNaN(field2)) {
field2 = 0
}
}
mgh_mgharish has done the right thing by calling the calculate function from the checkit function. But you can also just put it all in one function. (Also get around 1 or 2 extra problems you might have had as well with getElementById() ):
function checkit(_prefix,_thevalue)
{
if (_thevalue > 0 && _thevalue < 6)
{
var field1 = document.getElementById(_p
var field2 = document.getElementById(_p
var fv1 = field1.value;
var fv2 = field2.value;
if (fv1 == "" || isNaN(fv1))
fv1 = 0;
if (fv2 == "" || isNaN(fv2))
fv2 = 0;
field1.value = fv1;
field2.value = fv2;
}
else
alert("The value you have entered is not valid");
}
onchange="checkit('dar',th
Business Accounts
Answer for Membership
by: mgh_mgharishPosted on 2006-08-18 at 09:47:28ID: 17343885
function calc(thevalue){
if (thevalue == "" || isNull(thevalue)) {
thevalue = 0
alert(thevalue)
}