Py学习  »  Jquery

jquery tagit验证输入是否为数字

skazichris • 5 年前 • 1496 次点击  

我正在使用jquery标记。 我知道如何使用if和isnan使用javascript验证输入。 不过,如果有人教我如何验证输入标记,我会很感激的 如果不是数字,则将其删除:

以下代码对我不起作用 (仍接受字母数字而不是仅接受数字):

    var $tagInp13 = $("#oneSessionInstanceDetails");
        $tagInp13.tagit({
            allowSpaces: true,
            fieldName: "oneSessionInstanceDetails[]",
            preprocessTag: function (val) {
                if (!val) {
                    return '';
                }
                var values = val.split(/[\s,;]+/);
                //var values = val.split(",");//.split(";");
                if (values.length > 1) {
                    for (var i = 0; i < values.length; i++) {
                        $tagInp13.tagit("createTag", values[i]);
                    }
                    return ''
                } else {
                    return val
                }
            },
/* remove the tag if it is not numeric*/

            tagsChanged: function(tagValue, action, element){
                if (action == 'added'){
                    if (isNaN(tagValue)){
                        $tagInp13.tagit("remove", 'tag', tagValue);

                    }
                }
            }
        });
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43097
 
1496 次点击  
文章 [ 2 ]  |  最新文章 5 年前
skazichris
Reply   •   1 楼
skazichris    6 年前

我是这样解决的,经过反复验证:

var $tagInp13 = $("#oneSessionInstanceDetails");
    instanceNumbers =  ["1", "2", "3", "4", "5", "6", "7", "8"];
    $tagInp13.tagit({
        allowSpaces: true,
        onlyAvailableTags: true,
        autocomplete: {delay: 0,minLength: 0},
        availableTags: instanceNumbers,
        afterTagAdded: function (event, ui) {
            if ($.inArray(ui.tagLabel, instanceNumbers) == -1) {
                $("#oneSessionInstanceDetails").tagit("removeTagByLabel", ui.tagLabel);
            }
        },
        fieldName: "oneSessionInstanceDetails[]",
        preprocessTag: function (val) {
            if (!val) {
                return '';
            }
            var values = val.split(/[\s,;]+/);
            //var values = val.split(",");//.split(";");
            if (values.length > 1) {
                for (var i = 0; i < values.length; i++) {
                    $tagInp13.tagit("createTag", values[i]);
                }
                return ''
            } else {
                return val
            }
        }
    });
Patrick
Reply   •   2 楼
Patrick    6 年前
$.isNumeric( "-10" )

返回true。

https://api.jquery.com/jQuery.isNumeric/