最小和最大输入字段验证 [英] minimum and maximum Input field validation

查看:106
本文介绍了最小和最大输入字段验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为输入字段创建验证,这将在输入字段旁边显示一条消息.如您所见,用户不能输入500以下或800以上的任何内容

Trying to create validation for an input field, which will put a message alongside the input field. As you can see, the user cannot enter anything below 500 or anything above 800

<input type="text" name="amount" id="amount" maxlength="6" autocomplete="off"/>
<span class="paymentalert"></span>

请原谅我正在学习的JavaScript.

Please excuse my javascript as I'm still learning.

$(function(){
    var min = 500.00;
    var max = 800.00;
    if ("#amount" < 500.00){
         return ("Your payment must be between £500 and £800");
        else if ("#amount" > 800.00)
         return ("Your payment must be between £500 and £800");
    else return false;
    };

尝试使用Stackoverflow中的示例,但无济于事.

Trying to use examples from Stackoverflow but getting nowhere.

推荐答案

请参见演示.

$("#amount").bind("keyup keydown", function() {
    var amount = parseFloat($(this).val());
    if (amount) {
        if (amount < 500 || amount > 800) {
            $("span.paymentalert").html("Your payment must be between £500 and £800");
        } else {
            $("span.paymentalert").html("");
        }
    } else {
        $("span.paymentalert").html("Your payment must be a number");
    }
});

这篇关于最小和最大输入字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆