Jump to content

Adding three fields, must equal 12


twilitegxa

Recommended Posts

How can I write a function that adds the three fields in my form (body, soul, and mind), and makes sure they equal 12? If they don't equal 12 after they enter input into the three fields, I want to display  message saying the must equal 12. If they equal 12, no message should be displayed. Here is the section of my form:

 

<table border="0">
  <tr>
    <td>Body:</td>
    <td><input type="text" name="body" size="10" maxlength="2"></td>
  </tr>

  <tr>
    <td>Mind:</td>
    <td><input type="text" name="mind" size="10" maxlength="2"></td>
  </tr>
  <tr>
    <td>Soul:</td>
    <td><input type="text" name="soul" size="10" maxlength="2"></td>
  </tr>

 

They are more fields in the form, but I am currently only working with these three. How do I do this?

Link to comment
Share on other sites

I really don't know how to write the JavaScript part of this. Do I write it as a function like this:

 

<script type="text/javascript">

function total = (body+mind+soul);
if (total<>12)
  {
  document.write("<b>Total Must Equal 12</b>");
  }

</script>

 

I know I have done something wrong here, just not sure how to write it. And how do I set it to do this function on key up?

Link to comment
Share on other sites

I am pretty sure it would look like this:

 

<script type="text/javascript">

function total_bms(body, mind, soul)
{
total = body + mind + soul;
if (total<>12)
  {
  document.write("<b>Total Must Equal 12</b>");
  }
else
  {
  document.write("<b>Success!</b>");
  }
}
</script>

 

In usage:

 

total_bms(a, b, c);

 

Test it out and see if it works, I am by no means a Javascript expert.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.