Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Javascript Onblur If else function
    primarykey
    data
    text
    <p>This is my first week teaching myself javascript and i have run into a problem.</p> <p>I am trying to create a simple mortgage 'loan to value' calculator. My calculator works fine but I am trying to set up a minimum value validation for one of my two form fields.</p> <p>Here is a link to the page</p> <p><a href="http://alexmason.org/ltv.php" rel="nofollow">Link</a></p> <p>My code is below</p> <pre><code>function calculate (form) { var propVal = form.propertyValue.value; var loanAmt = form.mortgageAmount.value; var type = form.mortgageType.value; var ltv = loanAmt / propVal * 100 ; var roundedLtv = Math.floor(ltv); if ( type === "First Time Buyer" &amp;&amp; roundedLtv &lt;= 91 ) { document.getElementById("result").innerHTML= ("Your loan to value is: &lt;b&gt;" + (roundedLtv) + "%&lt;/b&gt; &lt;br /&gt; We can help place your mortgage"); } else if ( type === "Moving Home" &amp;&amp; roundedLtv &lt;= 91 ) { document.getElementById("result").innerHTML= ("Your loan to value is: &lt;b&gt;" + (roundedLtv) + "%&lt;/b&gt; &lt;br /&gt; We can help place your mortgage"); } else if ( type === "Remortgage" &amp;&amp; roundedLtv &lt;= 86 ) { document.getElementById("result").innerHTML= ("Your loan to value is: &lt;b&gt;" + (roundedLtv) + "%&lt;/b&gt; &lt;br /&gt; We can help place your mortgage"); } else if ( type === "Buy To Let Purchase" &amp;&amp; roundedLtv &lt;= 81 ) { document.getElementById("result").innerHTML= ("Your loan to value is: &lt;b&gt;" + (roundedLtv) + "%&lt;/b&gt; &lt;br /&gt; We can help place your mortgage"); } else if ( type === "Buy To Let Remortgage" &amp;&amp; roundedLtv &lt;= 76 ) { document.getElementById("result").innerHTML= ("Your loan to value is: &lt;b&gt;" + (roundedLtv) + "%&lt;/b&gt; &lt;br /&gt; We can help place your mortgage"); } else { document.getElementById("result").innerHTML= ("Your loan to value is: &lt;b&gt;" + (roundedLtv) + "%&lt;/b&gt; &lt;br /&gt; Unfortunatly we cannot help you place a mortgage"); } } </code></pre> <p>All works ok until this point, where i am trying to alert the user that the loan amount needs to be larger than 45000. Below in the HTML you will see im am trying to use the onBlur function</p> <pre><code>function loancheck (propVal,loanAmt,form) { if (loanAmt &gt;= 45000) { document.getElementById("val1").innerHTML= ("Thank You"); } else { document.getElementById("val1").innerHTML= ("Higher"); } } </code></pre> <p>This is the html to go with it</p> <pre><code>&lt;form name="myform" ACTION="" METHOD="GET"&gt; Mortgage Type &lt;select name="mortgageType"&gt; &lt;option&gt;First Time Buyer&lt;/option&gt; &lt;option&gt;Moving Home&lt;/option&gt; &lt;option&gt;Remortgage&lt;/option&gt; &lt;option&gt;Buy To Let Purchase&lt;/option&gt; &lt;option&gt;Buy To Let Remortgage&lt;/option&gt; &lt;/select&gt; &lt;br /&gt; Value &lt;input name="propertyValue" type="text" value=""/&gt;&lt;br /&gt; Mortgage Amount &lt;input name="mortgageAmount" type="text" value="" onBlur="loancheck(this.value)" /&gt; &lt;p id="val1"&gt;&lt;/p&gt; &lt;INPUT TYPE="button" NAME="button" Value="Click" onClick="calculate(this.form)"&gt; &lt;/form&gt; &lt;br /&gt;&lt;br /&gt; &lt;p id="result"&gt; &lt;/p&gt; </code></pre> <p>Im sure I'm doing something fundamentally wrong but if someone could point it out to me i would be most thankful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload