eccoti un esempio che potrebbe tornarti utile.. la funzione in questione evita che possano essere digitate lettere e accetta solo numeri.... potresti modificarla a tuo piacimento e fare un controllo sulla lunghezza del contenuto del campo

codice:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
 <head>
  <meta name="generator" content="PSPad editor, www.pspad.com" />
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script>
function numeralsOnly(evt) 
  {
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :((evt.which) ? evt.which : 0));
  if (charCode > 31 && (charCode < 48 || charCode > 57)) 
    {
    return false;
    }
  return true;
  }
</script>     
<title>
</title>  
 </head>
 <body>
<input type="text" name="prova" value="" onkeypress="return numeralsOnly(event)">     
 </body>

</html>