Solved

Simple Javascript Validation & "Sum" help

Posted on 2004-04-06
1
487 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
1 Comment
 
LVL 1

Accepted Solution

by:
bkiahg earned 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now