using javascript space validation for a textbox

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Sneha110
Forum Newbie
Posts: 1
Joined: Wed Nov 20, 2013 3:58 am

using javascript space validation for a textbox

Post by Sneha110 »

Hi frnds.. am new to php.. and my senior asked me to validate the 'Name' field such that user does not enter 'Space' in the beginning.. 'Space' in between their name is allowed.. i searched in google a lot.. but couldn't find the exact code which i need.. the codes which i tried showed a pop-up window when user enters 'Space'.. but i dont need like that.. what i need is, it should show just like a validated field.. i.e, "This field is required".. So can anyone please help me..

Thanks in advance!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using javascript space validation for a textbox

Post by requinix »

So they have to enter something and besides spaces?

Take the input, trim off the leading and trailing spaces, and make sure you've ended up with something that isn't just an empty string.
User avatar
Vincent Puglia
Forum Commoner
Posts: 67
Joined: Thu Sep 04, 2003 4:20 pm
Location: where the World once stood

Re: using javascript space validation for a textbox

Post by Vincent Puglia »

if you want a javascript solution:

Code: Select all

function doIt(obj)
{
  var notDone = true;
  do {
  if (obj.value.substr(0,1) == " ")
    obj.value= obj.value.substr(1);
  else notDone=false;
  } while(notDone)

Code: Select all

<form>
<input type='text' placeholder="First Last Name" onblur="doIt(this)">
</form>
Post Reply