日文半角全角判断

<!doctype html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”Generator” content=”EditPlus®”>
<meta name=”Author” content=””>
<meta name=”Keywords” content=””>
<meta name=”Description” content=””>
<title>Document</title>
</head>
<body>
<script>
// unless str.to_s =~ /^[ -~。-゚]*$/ # 半角のみOKなので、全角が混ざっているとfalseが返る
// unless str.to_s =~/^[^ -~。-゚]*$/ # 全角のみOKなので、半角が混ざっているとfalseが返る

function ishankaku(str) {

var pattern =/^[ -~。-゚]*$/;
if (pattern.test(str)) {
console.log(“半角”);
return true;
}
}

function iszenkaku(str){
var pattern =/^[^ -~。-゚]*$/;
if (pattern.test(str)) {
console.log(“全角”);
return true;
}
}
ishankaku(“カナ”);

iszenkaku(“山田”);

</script>
</body>
</html>

类似文章