Update main.js

Refactored validate function.