This is the first response on a google search on how to confirm that a string is valid css. So that's a pretty good answer. :) I would add rem and em to the regex. (\d*)(px|%|em|rem).
And depending on the situation, might want to check that \d is greater than or equal to 0, unless you want people to be able to say that something is -100px wide or something. And allow either '0' or 0.,