Skip to content

Instantly share code, notes, and snippets.

@mizuneko
Created September 26, 2018 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizuneko/8825cdec3b416a54f5c3b870629b9b71 to your computer and use it in GitHub Desktop.
Save mizuneko/8825cdec3b416a54f5c3b870629b9b71 to your computer and use it in GitHub Desktop.
[全角半角チェック] 「^\x01-\x7E」は1バイト文字でないもの。これだと半角カナもマッチするため半角カナの文字コード範囲「\xA1-\xDF」も指定。
/*******************************************************************************
文字列の全角/半角チェック
@param input String チェック対象文字列
@param charType String チェック種別
・"zenkaku" : 全角文字(ひらがな・カタカナ・漢字 etc.)
・"hiragana" : 全角ひらがな
・"katakana" : 全角カタカナ
・"alphanumeric" : 半角英数字(大文字・小文字)
・"numeric" : 半角数字
・"alphabetic" : 半角英字(大文字・小文字)
・"upper-alphabetic" : 半角英字(大文字のみ)
・"lower-alphabetic" : 半角英字(小文字のみ)
@return Boolean チェック結果OKかどうか
true : チェックOK(引数に指定した種別の文字列のみで構成されている)
false : チェックNG(引数に指定した種別以外の文字列が含まれている)
*******************************************************************************/
function checkCharType(input, charType) {
switch (charType) {
// 全角文字(ひらがな・カタカナ・漢字 etc.)
case "zenkaku":
return (input.match(/^[^\x01-\x7E\xA1-\xDF]+$/)) ? true : false;
// 全角ひらがな
case "hiragana":
return (input.match(/^[\u3041-\u3096]+$/)) ? true : false;
// 全角カタカナ
case "katakana":
return (input.match(/^[\u30a1-\u30f6]+$/)) ? true : false;
// 半角英数字(大文字・小文字)
case "alphanumeric":
return (input.match(/^[0-9a-zA-Z]+$/)) ? true : false;
// 半角数字
case "numeric":
return (input.match(/^[0-9]+$/)) ? true : false;
// 半角英字(大文字・小文字)
case "alphabetic":
return (input.match(/^[a-zA-Z]+$/)) ? true : false;
// 半角英字(大文字のみ)
case "upper-alphabetic":
return (input.match(/^[A-Z]+$/)) ? true : false;
// 半角英字(小文字のみ)
case "lower-alphabetic":
return (input.match(/^[a-z]+$/)) ? true : false;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment