Simplified code by using jQuery validate plugin.