Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code in javascript to validate whether the textBox is empty or not,
it works fine but after validating it jumps to next textbox, I want my cursor to be pointing on the same textBox untill he enters the correct input.

function trainNoName(txt)
{
  var trainNo=txt.value;
  if(trainNo=="")
  {
  alert(txt.name+" Field Can't be empty");
  document.getElementById("stFrm").focus();
  }
}



//i have used focus() in my last line but its not working.
Posted
Updated 16-Feb-12 5:55am
v4

Try this:-

function trainNoName(txt)
{
  var txtTN=document.getElementById(txt);
  var trainNo=txtTN.value;
  if(trainNo==''){
  alert('Train No Field Cannot be empty');
  document.getElementById(txtTN).focus();
  }
}


I hope this will work.
 
Share this answer
 
v3
Comments
The Doer 16-Feb-12 4:49am    
no its not working..:(
Varun Sareen 16-Feb-12 11:55am    
try my code again. I have modified a bit
fjdiewornncalwe 16-Feb-12 11:57am    
Sorry, couldn't help myself. I had to change can't to cannot in your literal string so that the syntax highlighting looks better. Cheers. +5.
Varun Sareen 16-Feb-12 12:00pm    
ohh thanks marcus..5 to you also
The Doer 20-Feb-12 6:37am    
no varun its not working.., am using mozilla..
Use your mind.


use if()...else....
 
Share this answer
 
v2
Comments
The Doer 16-Feb-12 4:53am    
eventhough its not.
I'll assume that txt is the input, since you are accessing txt.value and txt.name:
JavaScript
function trainNoName(txt) {
   var trainNo=txt.value;
   if(trainNo=="") {
      alert(txt.name+" Field Can't be empty");
      txt.focus();
   }
}
 
Share this answer
 
Comments
The Doer 20-Feb-12 6:41am    
no its not working..:(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900