Solved

Simple Javascript Validation & "Sum" help

Posted on 2004-04-06
1
483 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

746 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

13 Experts available now in Live!

Get 1:1 Help Now