Javascript – blank space validation

javascript

function validation(reg) {
    str = document.reg;
    if (str.name.value == "") {
        alert("Enter your name");
        str.name.focus();
        return false;
    }

Validation will working fine if input is empty.

Problem

  1. User can enter a blank space on the first.
  2. Also user can enter space only on the name.

How to prevent it?

Best Solution

Here are some trim functions.

http://www.somacon.com/p355.php

and using it

function validation(reg) {
    str = document.reg;
    if (str.name.value.trim() == "") {
        alert("Enter your name");
        str.name.focus();
        return false;
    }