You can use the mobile detect JS library to do this. Take note of the warning:
// WARNING:
// These JavaScript-based device detection features may ONLY work
// for the newest generation of smartphones, such as the iPhone,
// Android and Palm WebOS devices.
// These device detection features may NOT work for older smartphones
// which had poor support for JavaScript, including
// older BlackBerry, PalmOS, and Windows Mobile devices.
// Additionally, because JavaScript support is extremely poor among
// 'feature phones', these features may not work at all on such devices.
// For better results, consider using a server-based version of this code,
// such as Java, APS.NET, PHP, or Ruby.