String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); }; function myFunction(val){ if( '' != document.getElementById('to').value.trim()) { strEmailAddresses = document.getElementById('to').value + ", " + val; } else{ strEmailAddresses = val; } document.getElementById('to').value = strEmailAddresses.trim(); } function ValidateForm(){ if (document.getElementById('to').value == ''){ alert("Atleast one To email is required.") return false; } if (document.getElementById('from_email').value == ''){ alert("Your email is mandatory.") document.document.getElementById('from_value').focus(); return false; } if (document.document.getElementById('subject').value == ''){ alert("Subject is mandatory.") document.document.getElementById('subject').focus(); return false; } return true; }