Page 1 of 1

using javascript space validation for a textbox

Posted: Wed Nov 20, 2013 4:12 am
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!

Re: using javascript space validation for a textbox

Posted: Wed Nov 20, 2013 4:44 am
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.

Re: using javascript space validation for a textbox

Posted: Mon Dec 02, 2013 5:00 pm
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>