This is because on form error the form comes out of the cache table and skips fetching the form and all its _alter's. The 'best' practice I have found is to use a #pre_render function to drupal_add_js(). pre_render's are called always.