Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - blank space validation

Tags:

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?

like image 551
wow Avatar asked Jul 19 '09 22:07

wow


1 Answers

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;
    }
like image 195
Daniel A. White Avatar answered Oct 05 '22 02:10

Daniel A. White