Created
June 27, 2012 12:29
-
-
Save keroxp/3003774 to your computer and use it in GitHub Desktop.
自分の学籍番号と一文字違いの文字列を認識するプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
$(function(){ | |
var ID = $("#id").val(), | |
IDArray = [], | |
$input = $("#input2") | |
red = "#CD2525", | |
green = "#adff2f", | |
blue = "#1E90FF"; | |
for(var i = 0 , max = ID.length ; i < max ; i++){ | |
IDArray[i] = ID[i]; | |
} | |
$input.on("keydown keyup",checkID).val(ID).css("backgroundColor",green); | |
function checkID (e){ | |
var chars = this.value, | |
charsArray = []; | |
for(var i = 0 , max = chars.length ; i < max ; i++){ | |
charsArray[i] = chars[i]; | |
} | |
// abcd の一文字違いの定義 | |
// kbcd => 代入 | |
// abxcd => 挿入 | |
// abc => 欠如 | |
// 文字数でフィルタ | |
if( chars.length < ID.length - 1 || ID.length + 1 < chars.length){ | |
this.style.backgroundColor = red; | |
}else if(chars == ID){ | |
this.style.backgroundColor = green; | |
}else{ | |
var checked = false, | |
forCheck = [], | |
// -1 => 欠如, 0 => 代入 , 1 => 挿入 | |
type = chars.length - ID.length; | |
// 走査用の一時配列にIDをコピー | |
for(var i = 0 , max = IDArray.length ; i < max ; i++){ | |
forCheck[i] = IDArray[i]; | |
} | |
// 走査 | |
for(var i = 0 , max = chars.length ; i < max ; i++){ | |
// 連続が一致しなかった場合 | |
if(charsArray[i] != forCheck[i]){ | |
// それは一度目か? | |
if(!checked){ | |
checked = true; | |
switch(type){ | |
case -1 : | |
forCheck.shift(); | |
break; | |
case 0 : | |
// abcd | |
// dabc | |
if(i == 0){ | |
forCheck.unshift("_"); | |
} | |
break; | |
case 1 : | |
// abcd | |
// abcxd | |
// charsArray.splice(i,1); | |
forCheck.splice(i,1,"_",forCheck[i]); | |
break; | |
default : | |
break; | |
} | |
}else{ | |
// 二度目ならば一致しないとして中止 | |
this.style.backgroundColor = red; | |
break; | |
} | |
} | |
this.style.backgroundColor = blue; | |
} | |
} | |
} | |
}) | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment