Well, since you made the dashes optional in the RegExp, I would do it this way:
Code:
function validateField ()
{
var val = this.value.replace(/\D/g, "" ); // zap all NON-digit characters
if ( val.length != 7 ) /* then there must be 7 digits exactly */
{
this.value = '';
alert("not a vln");
return false; // ?? may not be needed, can't hurt
}
this.value = val.substr(0,2) + "-" + val.substr(2,2) + "-" + val.substr(4);
return true;
}