Trim in javascript

if (theForm.LastName.value.replace(/^\s+|\s+$/g, "") == "")
{
alert("Please enter a value for the \"LastName\" field.");
theForm.LastName.focus();
return (false);
}