0

这有一些错误,你们能告诉我我做错了什么吗

function verifyGroup(groupVal, errorid) {

    groupVal = $.trim(groupVal);

    if (groupVal != '') {

        var splChars = "*|,\":<>[]{}`\';()@&$#%!+-";
        for (var i = 0; i < groupVal.length; i++) {
            console.log(groupVal.charAt(i)+' == '+splChars.indexOf(groupVal.charAt(i)));

            if (splChars.indexOf(groupVal.charAt(i)) != -1) {

                $("#" + errorid).addClass("form-error").html("Illegal characters detected!");
                return false;
            } else {
                $("#" + errorid).removeClass("form-error").html("");
                return true;
            }
        }
    } else {
        $("#" + errorid).addClass("form-error").html("Group name should not be empty");
        return false;
    }
}

演示

4

2 回答 2

1

使用正则表达式

function verifyGroup(groupVal, errorid) {

    groupVal = $.trim(groupVal);
    console.log(groupVal);
    console.log(errorid);
    if (groupVal != '') {

        var regex = /[*|,\\":<>\[\]{}`';()@&$#%!+-]/;
        if(regex.test(groupVal)){
            $("#" + errorid).addClass("form-error").html("Illegal characters detected!");
            return false;
        } else {
            $("#" + errorid).removeClass("form-error").html("valid");
            return true;
        }
    } else {
        $("#" + errorid).addClass("form-error").html("Group name should not be empty");
        return false;
    }
}
$(function() {
    // Handler for .ready() called.

    $('#submit').click(function(){
        verifyGroup($('#ipId_create').val(), 'error_id');
    });
});

演示:小提琴

于 2013-08-05T06:42:14.493 回答
0

比较-1应该==不是!=

于 2013-08-05T06:42:38.010 回答