Of course employers are allowed to ask the question; but they can’t force an answer. If a potential employee doesn’t like such a policy, they are free to take their labor elsewhere...
I suppose if the employer is a public entity, then Constitutional objections could probably be raised...