次のコードは、文字 A ~ F と数字 0 ~ 9 と 3 つのダッシュ ("-") で構成される 35 文字で構成される入力によって送信されたアイテム コードをチェックします。有効な商品コードの例: 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
}
以下は、アイテム コードが 35 文字の長さでダッシュが含まれていない場合を除いて、うまく機能します。1 つまたは 2 つのダッシュが含まれている場合、このコードはそれをキャッチしますが、含まれているダッシュが 0 の場合はハングして何もしません。私はほぼすべてを試しましたが、解決策が何であるかを理解できないようです。長さが null であるため、ハングするだけです。何らかの方法で微調整する必要がある部分は次のとおりです。
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();
});
}
解決策は簡単だと思いますが、困惑しています。
編集:CSSを除いて、ほとんどの部分をすべてレイアウトした方法は次のとおりです。 http://jsfiddle.net/86KcG/1/