twilitegxa Posted July 21, 2009 Share Posted July 21, 2009 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? Quote Link to comment Share on other sites More sharing options...
ldougherty Posted July 21, 2009 Share Posted July 21, 2009 http://www.w3schools.com/jS/js_operators.asp total = body+mind+soul; http://www.w3schools.com/JS/js_if_else.asp if (total<>12) { document.write("<b>Total Must Equal 12</b>"); } Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 21, 2009 Author Share Posted July 21, 2009 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? Quote Link to comment Share on other sites More sharing options...
kittrellbj Posted July 21, 2009 Share Posted July 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 21, 2009 Author Share Posted July 21, 2009 It looks like it would work, but I am no expert either. It's not working for some reason. How would I add the function? Do I add it to each field (body, mind, and soul) and put onKeyUp? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.