私の理解が正しければ、フィールド値の検証が必要であり、要件は値が7514
やのような 4 つの数字から始まる必要があること9268
です。ここでは、次のような正規表現を使用して入力値を検証できます。
// Will work for " 123433 " or "12345634 ", etc.
var value = $(this).val(),
re = /^\s*(\d{4})(\d+)\s*$/, // better to initialize only once somewhere in parent scope
matches = re.exec(value),
expectedCode = 3541,
expectedLength = 13;
if(!!matches) {
var code = matches[1]; // first group is exactly first 4 digits
// in matches[2] you'll find the rest numbers.
if(value.length == expectedLength && code == expectedCode) {
// Change the color...
}
}
また、要件が 13 の長さに厳密である場合は、正規表現を次のように変更できます。
var re = /^(\d{4})(\d{9})$/;
最初のグループで最初の 4 つの数字を取得し、2 番目のグループで残りの 9 を取得します。
var matches = re.exec(value);
if(!!matches) {
var first4Digits = matches[1],
rest9Digits = matches[2];
// ...
// Also in this way you'll not need to check value.length to be 13.
}