Click to See Complete Forum and Search --> : Manipulate the cursor
janvanheerden
12-26-2002, 12:42 PM
Hi everybody,
I have a form with a number of textboxes. In the one textbox the user must enter a positive number. I validate the input for a positive number using the onChange event procedure.
It all works fine, except that the cursor is in another textbox after the validation message. How can I get the cursor back into the textbox that triggered the validation?
Thanks,
Jan
Do you need something like that?
<script language="javascript">
document.yourform.fieldname.focus();
</script>
janvanheerden
12-26-2002, 09:54 PM
Thanks Swon - I will try it
Jan
janvanheerden
12-28-2002, 10:22 PM
Hello Swon,
I can't seem to get it to work. I call function DN() from an onChange event in the DegrNo textbox. Everything else works OK. Any idea what I am doing wrong?
Thanks,
Jan
function DN(){ // Check if DegrNo is a +ve no or zero
var D = document.Form1.DegrNo.value
if(isNaN(D,10)){ // Check if user entered a number
alert("Please enter a positive number or leave as zero if you don't want this check done")
document.Form1.DegrNo.value = 0
document.Form1.DegrNo.focus() // Return cursor to this textbox
}
if(parseInt(D,10)<0){ // Check if number is zero or +ve
alert("Please enter a positive number or leave as zero if you don't want this check done")
document.Form1.DegrNo.value = 0
document.Form1.DegrNo.focus() // Return cursor to this textbox
}
}
Hi Jan,
let me know if it is not what you want:
<script language="JavaScript">
<!--
function DN(){ // Check if DegrNo is a +ve no or zero
var D = document.Form1.DegrNo.value
if(isNaN(D,10)){ // Check if user entered a number
alert("Please enter a positive number or leave as zero if you don't want this check done")
document.Form1.DegrNo.value = 0
document.Form1.DegrNo.focus() // Return cursor to this textbox
return false;
}
if(parseInt(D,10)<0){ // Check if number is zero or +ve
alert("Please enter a positive number or leave as zero if you don't want this check done")
document.Form1.DegrNo.value = 0
document.Form1.DegrNo.focus() // Return cursor to this textbox
return false;
}
}
//-->
</script>
</head>
<body>
<form name=Form1 action="" onSubmit="return DN()">
<textarea name=DegrNo></textarea>
<input type="Submit" name="" value="go">
</form>
</body>
janvanheerden
12-29-2002, 11:31 AM
Hi Swon,
When completing a form, I think it is better to validate each input as soon as it is done, rather than wait to do all the validations when the form is submitted. Especially, when you have a long form like mine.
I don't want my clients to get hit by one alert message upon another when he/she submits the form. They may just hit the Cancel button instead.
So, I want to validate prior to submission which works fine. Only problem, I can't get the cursor to return to the text box which activated the alert message.
I know I am doing something stupid somewhere, I just can't figure out what!!
Pse help.
Jan.
PS This site is driving me to drink and bad women!!
Jan, put the whole form inclusive jscript here, maybe we can figure it out!
michaelg
12-30-2002, 11:18 AM
This is exactly what I need to.
Get back to the same field where entry / error occurred.
Watching and learning.
Thanks!
khalidali63
12-30-2002, 12:30 PM
Hi Jan,
I hope the code segment below is close to what you want.
Give it try
<body>
<form name="Form1">
<input type="Text" name="DegrNo_1" onkeyup="DN('DegrNo_1');"></input>
<input type="Text" name="DegrNo_2" onkeyup="DN('DegrNo_2');"></input>
<input type="Text" name="DegrNo_3" onkeyup="DN('DegrNo_3');"></input>
</form>
<script>
function DN(fieldName){ // Check if DegrNo is a +ve no or zero
var obj = eval('document.Form1.'+fieldName);
var D = obj.value
if(isNaN(D,10)){ // Check if user entered a number
alert("Please enter a positive number or leave as zero if you don't want this check done")
obj.value = 0
obj.focus() // Return cursor to this textbox
}else if(parseInt(D,10)<0){ // Check if number is zero or +ve
alert("Please enter a positive number or leave as zero if you don't want this check done")
obj.value = 0
obj.focus() // Return cursor to this textbox
}
}
</script>
</body>
Cheers
Khalid
janvanheerden
01-04-2003, 06:50 AM
Thanks Khalid, I will try it
Jan