Results 1 to 2 of 2

Thread: Validate data in a function

  1. #1
    Join Date
    May 2013

    Validate data in a function

    I have a form with 3 fields. When the user hits the submit button, it calls a function which calculates the 3 fields together. I also have functions to validate the data in each field. Currently the validation checks that the field is filled in and that the numbers aren't too high or too low. I would also like it to check that the values in each field are actual numbers. I tried using isNaN() but that doesn't seem to be working.

    Also, when the form is submitted, I want it to run each of the validation functions and then calculate the results ONLY if there were no errors. If tried setting it up as if/else- with each of the validation returning false if there's an error and true if it's all good, then the calculation function checking if each function is true before performing the math but that doesn't seem to be working either. I'd appreciate a little insight here!

    Here are a link to the files
    (working files)

    http://tiger.towson.edu/~tbaldw2/loa...r/LoanCalc1.js (with the non-working changes)

  2. #2
    Join Date
    May 2006
    Somewhere behind your screen
    <!DOCTYPE html>
    <!-- Built in CoffeeCup HTML Editor -->
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
       <title>Loan Payment calculator</title>
       <script type="text/javascript">
    function doc(id){return document.getElementById(id);}
    function numsonly(obj){
    var val=obj.value,mask=/[^\d]/ig;
    function checkFields(frm){
    var fields=frm.elements,valid=true;
    for(var i in fields){
    var fid=fields[i].id,val=(doc(fid).value.length>0)?new Number(doc(fid).value):0;
    if(fid==='amt'){if(val<10 || val>1000000){doc(fid+'_err').innerHTML='Please enter a number between 10 and 1000000';valid=false;doc(fid).value='';doc(fid).focus();break;}}
    if(fid==='apr'){if(val<0.1 || val>20){doc(fid+'_err').innerHTML='Please enter a number between .1 and 20';valid=false;doc(fid).value='';doc(fid).focus();break;}}
    if(fid==='mtp'){if(val<1 || val>600){doc(fid+'_err').innerHTML='Please enter a number between 1 and 600.';valid=false;doc(fid).value='';doc(fid).focus();break;}}
    function init(){
    function docalc(){
        var amount = doc('amt').value;   //Sets the value from the "amount of loan" field to the variable amount
        var apr = doc('apr').value; 		   //Sets the value of the apr field to the apr variable
        var months = doc('mtp').value;   //Sets the months to pay field value to the months variable
    	var monthly_int = apr/1200.0; /* interest divided by 12 months * 100 percent */
    	var pvifa=(1.0/ monthly_int)-(1.0/( monthly_int *Math.pow(1.0+ monthly_int, months)));
    	payment = amount/pvifa;
    	payment = Math.round(payment*100)/100 //Rounds the "payment" number to 2 decimal places 
    	//return false;
       <link rel="stylesheet" href="LoanCalc.css"/>
    <h1>Loan Calculator</h1>
    <form name="loandata" action="about.blank" method="get"> <!-- Start of the form which will take input for the loan calculator -->
    <div id="description"
    <p>This form will calculate how much monthly payments need to be in order to pay off a loan. Fill in the fields below, numbers and decimal points only, and click Calc Payment to calculate the monthly payment. Enter new numbers and click Calc Payment again to get an updated monthly payment. </p>
       <li>For Amount enter a number between 100 and 1000000</li>
       <li>For APR enter a number between 0.1 and 20</li>
       <li>For Months enter a number between 1 and 600.</li>
    </div> <!-- creating a div to make extra space below the description and before the table--> 
      <table> <!-- Table to keep the titles and form fields layed out nicely -->
    	  <th colspan="4">Enter Loan Information:</th>
          <td class="label"><label for="amt">Loan Amount:</label></td>
          <td><input type="text" name="amount" id="amt"></td> <!-- onBlur means when the field is deselected, will run the validate function to ensure there are no invalid characters in the field -->
    	  <td class="error" id="amt_err"><!-- This table cell will contain an error message if invalid info is entered in the field. It has a class of error so all the cells with this purpose can get the same formatting, and a unique id so the function knows which table cell to put the text in--></td> 
          <td class="label"><label for="apr">Annual Percentage Rate (APR):</label></td>
          <td><input type="text" name="apr" id="apr">% </td>
    	  <td class="error" id="apr_err"></td>
          <td class="label"><label for="mtp">Months to pay:</label></td>
          <td><input type="text" name="months" id="mtp"></td>
    	  <td class="error" id="mtp_err"></td>
    	  <td class="button"><input type="button" value="Calc Payment" onclick="checkFields(this.form)"></td> <!-- Calls function OnSubmit which takes the numbers from each field and calculates them to give a total -->
    	  <td class="button"><input type="reset" value="Reset"></td> <!-- Reset button to clear the form fields -->
      <h2>Payment Information:</h2>
      <p>Your monthly payment will be: $<span id="results"> <small id="note"> Fill in the fields above and click Calc Payment to have this figure calculated.</small></span><p> 
      <!-- The OnSubmit function will overwrite the text in the span above with the results of the calculation -->
    <div id="footer">
     <small> <span class="info">Student ID: 0129508</span> <span class="info">&copy;2013 Tenley Baldwin</span></small>
    signature under construction

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10810 seconds
  • Memory Usage 2,860KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates