Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple Javascript Validation & "Sum" help

Posted on 2004-04-06
1
Medium Priority
?
501 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

885 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