Code:
<script>
function checkForValidName( fld )
{
var re = /^[a-z]+\_[a-z]+$/i
// optional: Trim spaces from ends first:
var txt = fld.value.replace( /(^\s+|\s+$)/g, "" );
// optional: Put the trimmed value back in place
fld.value = txt;
if ( ! re.test( txt ) )
{
alert("Must be in form firstname_lastname");
fld.focus();
fld.select();
return false;
}
return true;
}
</script>
Then invoke that function via something like
<form onsubmit="return checkForValidName( this.SomeFieldName );">
Note that this RE will allow names as simple as
a_b
If you want to insist on (say) a minimum of 3 letters in first name and 2 in second, you could do
var re = /^[a-z]{3,99}\_[a-z]{2,99}$/i
Or use whatever numbers you feel comfortable with.