Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple Javascript Validation & "Sum" help

Posted on 2004-04-06
1
Medium Priority
?
498 Views
Last Modified: 2012-08-13
Just starting out with javascript.  I figured out how to get the fields to sum but wanted to see how someone else would do it.  I would also like to see how to validate the form so that the dollar sign and decimal point gets added in to the currency fields but still "add" the result to the total field with the dollar sign.  It would be great to have it so only numeric data can be entered in.  Thank you...below is what I have...feel free to tweak anything you wish but if you could leave the field names the same I would appreciate it.

<html>
<head>

<script language="JavaScript">
function addForm(){
document.form.req_TotalRaisedforWeek.value = document.form.req_TotalFundsRaisedatRegisters.value * 1 + document.form.req_WeekendEventTotalAmountRaised.value *1;

</script>

</head>
<body>
<form enctype="multipart/form-data" name="form">
    <center>
    <table WIDTH="600" BORDER=0 align="left" >

      <tr>

        <td HEIGHT="29" colspan="2">
          <h1>Javascript Test Form</h1></td>
      </tr>
      <tr>
        <td width="55%" height="28" bgcolor="#FFFFCC"><font color="#000000"><b>Person Submitting: <font color="#FF0000">*</font></b> </font></td>
        <td width="45%">
          <div align="left">
            <input name="req_PersonSubmitting" id="req_PersonSubmitting">
          </div></td>
      </tr>
      <tr>
        <td height="28" nowrap bgcolor="#FFFFCC"><strong>Total Funds Raised at
            Registers (Canisters):<font color="#000000"><font color="#FF0000">*</font></font></strong></td>
        <td>
          <div align="left">
            <input name="req_TotalFundsRaisedatRegisters" id="req_TotalFundsRaisedatRegisters" onChange="addForm();" size="6" maxlength="6">
          </div></td>
      </tr>
      <tr>
        <td height="28" bgcolor="#FFFFCC"><strong> Weekend
            Event Description: <font size="2">(if none, type none)</font><font color="#000000"><font color="#FF0000">*</font></font></strong></td>
        <td>
          <div align="left">
            <input name="req_WeekendEventDescription" id="req_WeekendEventDescription" onChange="addForm();" size="20" maxlength="6">
          </div></td>
      </tr>
      <tr>
        <td bgcolor="#FFFFCC"><strong>Weekend Event Total Amount Raised:<font color="#000000"><font color="#FF0000">*</font></font></strong></td>
        <td bgcolor="#FFFFFF">
          <div align="left">
            <input name="req_WeekendEventTotalAmountRaised" id="req_WeekendEventTotalAmountRaised" onChange="addForm();" size="6" maxlength="6">
          </div></td>
      </tr>
      <tr bgcolor="#FFFFFF">
        <td height="3" colspan="2"><br></td>
      </tr>
      <tr>
        <td height="28" nowrap bgcolor="#FFFFCC"><strong><font color="#000000">Total Raised for Week: <font size="2">(automatically
          calculated)</font><font color="#FF0000">*</font></font></strong></td>
        <td>
          <div align="left">
            <input name="req_TotalRaisedforWeek" id="req_TotalRaisedforWeek" size="7" maxlength="7" onFocus="blur()">
          </div></td>
      </tr>
      <tr>
        <td height="28" colspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td height="28" colspan="2"><input type="submit" value="Submit" onClick="addForm();" ></td>
      </tr>
    </table>

    <br>
    </center>

</form>
</body>
</html>
0
Comment
Question by:xcusmwah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
bkiahg earned 200 total points
ID: 10770207
Try this

<script language="JavaScript">
function addForm()  {
  var isValid  = false;
  var isValid2 = false;
  var req_TotalFundsRaisedatRegisters   = document.form.req_TotalFundsRaisedatRegisters.value
  var req_WeekendEventTotalAmountRaised = document.form.req_WeekendEventTotalAmountRaised.value
  var ccCheckRegExp = /[^\d\.]/;
  isValid  = !ccCheckRegExp.test(req_TotalFundsRaisedatRegisters);
  isValid2 = !ccCheckRegExp.test(req_WeekendEventTotalAmountRaised);

  if (!isValid)  {
    alert("Must enter a number");
    document.form.req_TotalFundsRaisedatRegisters.value = "";
    document.form.req_TotalFundsRaisedatRegisters.focus();
  }
  else if (!isValid2)  {
    alert("Must enter a number");
    document.form.req_WeekendEventTotalAmountRaised.value = "";
    document.form.req_WeekendEventTotalAmountRaised.focus();
  }
  else  {
    var total = req_TotalFundsRaisedatRegisters * 1 + req_WeekendEventTotalAmountRaised *1;
    document.form.req_TotalRaisedforWeek.value = "$" + total
  }
}
</script>
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question