move form validation into a separate function