2015-11-24 83 views
-2

基本上我試圖用JavaScript驗證這個頁面,然後將它提交到下一頁,但代碼根本不起作用。當所有字段填寫正確後,頁面不會進入下一頁。驗證碼也是如此。用數字填充高度,但它仍然顯示提醒輸入我的身高的數字。爲什麼我的驗證碼不起作用?

這裏有一個fiddlejs https://jsfiddle.net/aL7q853L/1/

function validation() { 
    var height, weight; 
    height = parseFloat($("#height").val()); 
    weight = parseFloat($("#weight").val()); 

    if (validated(height, weight)) { 
     document.testform.submit(); 
    } 
} 

function validated(height, weight) { 
    if (height == "") { 
     alert("Please enter your height in meters!"); 
     $("#height").focus(); 
     return false; 
    } 
    else if (weight == "") { 
     alert("Please enter your weight in kg!"); 
     $("#weight").focus(); 
     return false; 
    } 
    else if (isNaN(height)) { 
     alert("Height must be a number!"); 
     $("#height").focus(); 
     return false; 
    } 
    else if (isNaN(weight)) { 
     alert("Weight must be a number!"); 
     $("#weight").focus(); 
     return false; 
    } 
    else if (height <= 130) { 
     alert("Height entered must be more than 0 meters!"); 
     $("#rate").focus(); 
     return false; 
    } 
    else if (weight <= 30) { 
     alert("Weight entered must be more than 0kg!"); 
     $("#weight").focus(); 
     return false; 
    } 
    return true; 
} 

而這裏的身體

<label for="height">Height (In meters)</label> 
<input type="text" name="height" id="height" placeholder="enter height in meters"> 
<label for="weight">Weight</label> 
<input type="text" name="weight" id="weight" placeholder="enter weight in KG"> 
+0

*你可以提供一個小提琴顯示這種行爲完全不*工作? atm你的描述是有點...不確定... –

+2

你怎麼稱呼驗證? –

+0

你能提供一個小提琴嗎? – OpuLance

回答

0

這工作得很好。

<label for="weight">Weight</label> 
<input type="text" onchange="validation()" name="weight" id="weight" placeholder="enter weight in KG"> 



validation = function() { 
    var height, weight; 
    height = parseFloat($("#height").val()); 
    weight = parseFloat($("#weight").val()); 

    if (validated(height, weight)) { 
     document.testform.submit(); 
    } 
} 

function validated(height, weight) { 
    if (height == "") { 
     alert("Please enter your height in meters!"); 
     $("#height").focus(); 
     return false; 
    } else if (weight == "") { 
     alert("Please enter your weight in kg!"); 
     $("#weight").focus(); 
     return false; 
    } else if (isNaN(height)) { 
     alert("Height must be a number!"); 
     $("#height").focus(); 
     return false; 
    } else if (isNaN(weight)) { 
     alert("Weight must be a number!"); 
     $("#weight").focus(); 
     return false; 
    } else if (height <= 130) { 
     alert("Height entered must be more than 0 meters!"); 
     $("#rate").focus(); 
     return false; 
    } else if (weight <= 30) { 
     alert("Weight entered must be more than 0kg!"); 
     $("#weight").focus(); 
     return false; 
    } 
    return true; 
} 
0

在你的js腳本添加

$(function() { 
function validation() { 
      var height, weight; 
      height = parseFloat($("#height").val()); 
      weight = parseFloat($("#weight").val()); 

      if (validated(height, weight)) { 
       document.testform.submit();   
      }   
    } 

相關問題