I believe this API is the best to use for checking if the Gamertag is valid or not.
Here is a valid Gamertag: https://xboxapi.com/profile/VNM+Jar that returns "Success":true
Here is an invalid Gamertag: https://xboxapi.com/profile/196jjdfd74nu9za that returns "Success":false
You could then do anything you wish with these values...