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
https://forums.phpfreaks.com/topic/166717-adding-three-fields-must-equal-12/
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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.