为什么我的验证码不起作用?

时间:2015-11-24 12:54:54

标签: javascript validation

基本上我正在尝试使用javascript验证此页面,然后将其提交到下一页,但代码根本不起作用。当所有字段都已正确填写后,页面不会进入下一页。验证码也是不确定的。用数字填充高度,但它仍然显示以数字输入我的身高的警报。

这是一个小提琴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">

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

添加

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

            if (validated(height, weight)) {
                document.testform.submit();         
            }           
    }
你的js脚本中的