Restrict total selected options with MultiSelect

Is there a way to restrict MultiSelect so that only, say, 5 options total can be selected? I know I can restrict afterwards but I didn’t know if there was some way to do it with the component itself. Thanks!