简体   繁体   English

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

[英]Why does my validation code not work?

Basically I'm trying to validate this page with javascript then submit it on to the next page, but the code doesn't work at all.基本上我正在尝试使用 javascript 验证此页面,然后将其提交到下一页,但代码根本不起作用。 When all fields have been filled in properly, the page doesn't go onto the next page.正确填写所有字段后,该页面不会进入下一页。 Validation code is iffy as well.. Filled in height with numbers but it still shows the alert to enter my height in numbers.验证代码也是不确定的。 用数字填充高度,但它仍然显示警告以数字输入我的高度。

Here's a fiddlejs https://jsfiddle.net/aL7q853L/1/这是一个 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;
}

And here's the body这是身体

<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">

This works fine.这工作正常。

<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;
}

add添加

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

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

in your js script在你的 js 脚本中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM