check if val is defined before overwriting

When a validation error occured in form, undefined value is stored in localStorage instead of keeping last valid value known