如果长度为空则需要传递条件

Mat*_* W. 5 javascript regex null jquery if-statement

下面检查通过由35个字符组成的输入提交的项目代码,包括字母AF和数字0-9,以及三个破折号(" - ").有效项目代码的示例如下:16FA860F-E86A457B-A28A238B-2ACA6E3D

//Checks the item code to see if it meets requirements
if($("#input").val().length > 35) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> is too long.<br>");

    $("#ise").each(function(){
    this.reset();
    });
}
else if($("#input").val().length < 35) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> is too short. Be sure to include dashes.<br>");

    $("#ise").each(function(){
    this.reset();
    });
}
else if($("#input").val().match(/([^A-Fa-f0-9-]+)/gm)) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> contains invalid characters.<br>");

    $("#ise").each(function(){
    this.reset();
    });
}

else if($("#input").val().match(/[-]/g, "").length > 3) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> is an invalid format. Please only use 3 dashes.<br>");

    $("#ise").each(function(){
    this.reset();
    });
}
else if($("#input").val().match(/[-]/g, "").length < 3) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> is an invalid format. Please include 3 dashes.<br>");

    $("#ise").each(function(){
    this.reset();
    });
} 

else {
 //Rest of my code
}
Run Code Online (Sandbox Code Playgroud)

以下情况很有效,除非项目代码长度为35个字符,但不包含短划线.如果它包含1或2个破折号,则此代码捕获它,但如果它包含0,则它只是挂起并且什么都不做.我已经尝试了一切,但似乎无法弄清楚解决方案是什么.由于长度为空,它只是挂起.应该调整的部分是这样的:

else if($("#input").val().match(/[-]/g, "").length > 3) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> is an invalid format. Please only use 3 dashes.<br>");

    $("#ise").each(function(){
    this.reset();
    });
}
else if($("#input").val().match(/[-]/g, "").length < 3) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> is an invalid format. Please include 3 dashes.<br>");

    $("#ise").each(function(){
    this.reset();
    });
} 
Run Code Online (Sandbox Code Playgroud)

我确信解决方案很简单,但我很难过.

编辑:除了CSS之外,我在大多数情况下都是如何布局的. http://jsfiddle.net/86KcG/1/

sai*_*ama 1

您可以通过使用正则表达式来修复您的代码,就像这样

/^[A-F0-9]+\-[A-F0-9]+\-[A-F0-9]+\-[A-F0-9]+$/
Run Code Online (Sandbox Code Playgroud)

其中 ^ 和 $ 匹配输入的开始和结束,以及字符 AF 和 0-9 组,并限制破折号由 AF 组或 0-9 组中的至少一个字符分隔。

将此检查与 35 个字符的长度检查相结合可以使您的代码正常工作。

//Checks the item code to see if it meets requirements
if($("#input").val().length != 35) {
    $("#errorLogContent").prepend("The item code <font color='#FFFFFF'>" + itemCode + "</font> is too long/short.<br>");

    $("#ise").each(function(){
        this.reset();
    });
}
else if(!(/^[A-F0-9]+\-[A-F0-9]+\-[A-F0-9]+\-[A-F0-9]+$/.test($("#input").val()))) {
    $("#errorLogContent").prepend("Insert some dashes and make sure the required pattern...<br>");

    $("#ise").each(function(){
        this.reset();
    });
}
Run Code Online (Sandbox Code Playgroud)