2016-04-09 23 views
0

私はPoperty値と前払いを検証しようとしています。正 - - 全体の数 少なくとも65,000ドル以上でなければならないことを頭金関数内に間違ったエラーメッセージが出力される

が は数値でなければなりません存在している必要があります

プロパティ値を:条件は以下の通りです。 - 正 - 整数 プロパティの値の少なくとも20%(propValue)でなければなりません

My機能がある(ソートの)

が は数値でなければなりません存在している必要があります:頭金

ワーキング。すべての検証テストに合格するわけではありません。誰かがこれを改善する方法について正しい方向で私を指すことができるなら、それは非常に高く評価されるでしょう。ダウン賃金と値のための私の2つの機能:

function propValueValidation(errMessages){ 

var propValueLength = document.mortgage.propValue.value.length; 

var propValueNumber = isNaN(document.mortgage.propValue.value); 

var propValue = document.mortgage.propValue.value; 

var downPayPlus = document.mortgage.downPay.value + 65000; 

if (!propValueLength) { 
    errMessages += " Property Value is a required field"; 
    return errMessages; 
} 
    else if (typeof propValue === 'number') { 
     var remainder = (propValue % 1); 
     if(remainder != 0){ 
      errMessages += "Property Value must be a positive whole number"; 
      return errMessages; 
     } 
    } 
    else if (propValue < downPayPlus){ 
     errMessages += "Property Value must be at least 65,000 greater than the down payment"; 
     return errMessages; 

    } 
    return errMessages; 
} 

//validate down pay 

function downPayValidation(errMessages){ 

var downPayLength = document.mortgage.downPay.value.length; 

var downPay = document.mortgage.downPay.value; 

var propValueMin = document.mortgage.propValue.value * 0.2; 

if (!downPayLength) { 
    errMessages += "Down Payment is a required field"; 
    return errMessages; 
} 
    else if (typeof downPay === 'number') { 
     var remainder = (downPay % 1); 
     if(remainder != 0){ 
     errMessages += "Down Payment must be a positive whole number"; 
     return errMessages; 
     } 
    } 
    else if (downPay < propValueMin){ 
     errMessages +="Down Payment must be at least 20% of the property value"; 
     return errMessages; 

    } 

    return errMessages; 
} 

HTML:

<label class="label">Property Value </label>  
     <input type="text" name="propValue" id="propValue" size="7" maxlength="6" > 

     <br> 

    <label class="label">Down Payment </label>  
     <input type="text" name="downPay" id="downPay" size="7" maxlength="6" > 

downpayは、それはまだ、たとえば、フォームを提出する "1nn1" であるとき。ありがとう!

+1

'document.mortgage.propValue.value'は、文字列ではなく、番号です。 – Xufox

+0

実際にこれらの関数を呼び出す場所はどこですか?あなたはどこにでも 'propValueNumber'値を使用していないことに気付きましたか? 'isNaN'関数は、文字列値が数値かどうかをチェックする目的ではありません。 – JLRishe

答えて

0

parseInt()を使用して番号を確認できます。

var tempVal = document.mortgage.propValue.value; 
var propValue = parseInt(tempVal); // this will try to extract an integer from tempVal 

if (tempVal != propValue.toString()) // if true, there were non-number chars or value is NaN 
{ 
    errMessages += "Bad value, please enter an integer"; 
} 
0

あなたは(入力値である)文字列値にisNaNを使用しないでください。代わりに、最初にこのような文字列を​​(またはparseFloat)に変換します。ただし、parseFloatまたはparseIntは、開始の数字を含む文字列を受け入れますが、Numberは入力全体を数字として解析する必要があります。次に、isNaNと呼んでください。 (入力要素のvalueプロパティは常に文字列であるので、それを得ることはありません)あなたは、数値データのための1つの枝を持っているので

は、またif elseロジックに問題があり、その上elseを比較すること他の金額の金額。しかし、最後のテストでは値は数値でなければなりません。そのテストは間違った場所にあります。ここで

が作ったいくつかの変更であなたの最初の関数のコードです:

function propValueValidation(errMessages){ 
    var propValue = document.mortgage.propValue.value; 
    var propValueNumber = Number(propValue); 
    var downPayPlus = propValueNumber + 65000; 
    var genericMsg = ' property value was provided. Please provide a positive' + 
       ' whole number, at least 65,000 greater than the down payment.\n'; 
    if (!propValue.length) { 
     errMessages += 'No' + genericMsg; 
    } else if (isNaN(propValueNumber)) { 
     errMessages += 'A non-numerical' + genericMsg; 
    } else if (propValueNumber % 1) { 
     errMessages += 'A fractional' + genericMsg; 
    } else if (propValueNumber < downPayPlus){ 
     errMessages += 'A too small' + genericMsg; 
    } 
    return errMessages; 
} 
関連する問題