Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8163299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:59:16+00:00 2026-06-06T18:59:16+00:00

I have a form that is basically a calculator. you can type an equation

  • 0

I have a form that is basically a calculator. you can type an equation in and it will evaluate it. I also have 2 memory fields(text boxes named m1 and m2) where you can type something in and it will hold that value and then when you are typing an expression in the first box, you can reference m1 or m2 in your equation and it will evaluate using the numbers you entered in the memory fields.

The problem is if you try to reference m1 or m2 in your equation and the text boxes are blank, You get an undefined error.

I’ve been spinning my wheels for hours to try and put some sort of check that if the equation is evaluated to undefined, just show a pop up box. I need this in raw javascript. any help is appreciated.

function displayResult(thisElement)
{
    thisElement.value = eval(thisElement.value); <!-- this line throws the error if you use m1 and no m1 is defined, m2 and no m2 is defined, etc -->
    if(!(thisElement.value>0))
    {
        thisElement.value=0;
    }
}

function mem(v) 
{
    v.value = eval(v.value);
    eval(v.name+"="+v.value);
}

<input id="calcFormula" name="calculate" size="40" />
<input type="submit" value="Calculate" onclick="displayResult(this.form.calculate);" />

<input name="m1" id="m1" size="12" onchange="mem(this);" value="0" />
<input name="m2" id="m2" size="12" onchange="mem(this);" value="0" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:59:18+00:00Added an answer on June 6, 2026 at 6:59 pm

    I can think of three solutions:

    1. You can make an assumption that empty m1/m2 means 0, so there will never be an undefined value. This really simplifies things.
    2. You can use regexp to check first for any occurrence of m1 or m2 in the equation and if it exists then check if is undefined.
    3. But the best method is to use try…catch.

    Try/Catch Example:

    try {
        eval('12+3+m1');
    } catch (e) {
        alert(e.message);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a report that is basically a form with multiple empty fields and
I have a form that sends money value e.g <input type=text name=amount value=N50,000.00 NGN
I have a form field that accepts large text from users. It is basically
I have a form wizard that contains 3 forms. Basically, what I am trying
I am working on some code, basically I have a form. On that form
I have a form that I'm submitting through AJAX. The form includes many fields,
I have a html form that is basically vertical but i really have no
I have a form that (amongst other things) contains 2 multi-select listboxes. Basically you
Say I have one form that changes its content (fields and options) based on
I basically have a form that you input chat messages into. It looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.