While you certainly could do a Javascript approach:
<script type="text/javascript">
function checkLength(obj, len) {
if(obj.value.length >= len) {
obj.value = obj.value.substring(0, len);
alert('fieldname must be ' + len + ' characters or less!');
return false;
} else return true;
}
</script>
<input type="text" name="fieldname" onkeypress="return checkLength(this, 20)">
devinemke's suggestion of an HTML approach (or combine both methods?) would probably be better simply because some people may not have Javascript enabled.