入力バイト数のチェック

javascript.dohow.jp

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>JavaScriptの練習</title>
</head>
<body>
<script type="text/javascript">
<!--
function getBytes(strSrc){
    var len = 0;
    strSrc = escape(strSrc);
    for(i = 0; i < strSrc.length; i++, len++){
        if(strSrc.charAt(i) == "%"){
            if(strSrc.charAt(++i) == "u"){
                i += 3;
                len++;
            }
            i++;
        }
    }
    return len;
}
// -->
</script>

<form name="myForm">
    <input name="text1" type="text" id="text1" size="40">
    <input type="button" onClick="alert(getBytes(document.myForm.text1.value));" value="バイト数は?">
<form>
</body>
</html>