0

I'd like to place the focus on the last character of the text after the whitespace validation. The focus doesn't end up on the last element. Please look into the code and suggest any changes to me.

var pattern=/^\s|\s$/;
var textentered=document.textentered.text.value;
if(textentered.match(pattern))
{
  alert("Spaces are not allowed");
  textentered.focus();
  return false;
}

Thanks

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
niks
  • 1,063
  • 2
  • 9
  • 18

1 Answers1

1

I think you want

document.textentered.text.focus();

Currently you are trying to focus on the value, not the element.

niks
  • 1,063
  • 2
  • 9
  • 18
davehale23
  • 4,374
  • 2
  • 27
  • 40
  • Ok. But will the focus be on the last charecter of the text if I use document.textentered.text.focus();. May be it will put the focus on the first charecter. – niks Mar 07 '13 at 19:10
  • I understand what you are saying now. See [this fiddle](http://jsfiddle.net/5NMPm/) for a quick example of how do do this. Also see [this SO post](http://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element) for extensive details. – davehale23 Mar 07 '13 at 20:52