Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2013, 07:45 PM   PM User | #1
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
setfocus()

Hi

I can seem to figure out how to set the focus back on a form box with an invalid entry.

Code:
function isFilledNum(number) {

	var number = parseInt(number.value)
	

if( number< 0 || number > 100 || isNaN(number) == true) 

{
alert("Enter a number between 0 and 100 ");
setfocus(document.getElementById(this));

}
I get the error "Uncaught TypeError: Cannot call method 'focus' of null".
trancecommunity is offline   Reply With Quote
Old 02-18-2013, 08:04 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
document.getElementById(this).focus(); might do it. I rarely use this for anything.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-18-2013, 08:10 PM   PM User | #3
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
Quote:
Originally Posted by WolfShade View Post
document.getElementById(this).focus(); might do it. I rarely use this for anything.
Returns

Uncaught TypeError: Cannot call method 'focus' of null

If i type in an invalid number.
trancecommunity is offline   Reply With Quote
Old 02-18-2013, 08:16 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
what is "this" meant to be referring to in your code?

and why not
Code:
document.getElementById("whatever_the_actual_id_of_the_input_is").focus();
?
xelawho is offline   Reply With Quote
Old 02-18-2013, 08:41 PM   PM User | #5
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Yeah, it really should be the actual id of the element. Pass it along as an argument.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-18-2013, 09:27 PM   PM User | #6
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
Would this still apply if i have several boxes that need verifying using the same function. i.e different test results. As each box is filled and clicked out of it calls the function to verify.

What would the id be in this case as it changes every box as far as i know?

I see it always reverts back to the 1st id used even if the 3rd or 4th box is wrong. Any way of making it change depending on box tested?

Last edited by trancecommunity; 02-18-2013 at 09:29 PM..
trancecommunity is offline   Reply With Quote
Old 02-18-2013, 09:37 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
1 <input type ="text" id = "text1" onblur = "check(this)";><br>
2 <input type ="text" id = "text2" onblur = "check(this)";><br>
3 <input type ="text" id = "text3" onblur = "check(this)";><br>

<script type = "text/javascript">
function check (which) {
var box = which.id;
var val = which.value;
// check that val is a number within a certain range
alert ("You have checked box " + box + " whose value is " + val);
if (val != "OK") {
document.getElementById(box).focus();
}
}
</script>
var number = parseInt(number.value); // two variables with the same name! In any case it is not a good idea to use the word number which can be confused with Number().
should be
var num = Math.floor(number.value);

parseInt() is used to convert numbers from one base to another, and if you use it you need to specify the radix (10).
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-18-2013 at 09:49 PM..
Philip M is offline   Reply With Quote
Old 02-18-2013, 09:43 PM   PM User | #8
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
Cheers for the reply but that doesnt setfocus back to the wrong box.

edit. sorry i see what you mean there when it just alerts where the problem is but it needs to setfocus.
trancecommunity is offline   Reply With Quote
Old 02-18-2013, 09:50 PM   PM User | #9
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
Might help if i add this.

Code:
<label for="mark1">Mark One</label>
 <input type="text" name="mark1" id="mark1" size="3" maxlength ="3" value="0" onblur="isFilledNum(this)">
</div>
<div>
<label for="mark2">Mark Two</label>
 <input type="text"  name="mark2" id="mark2" size="3" maxlength ="3" value="0" onblur="isFilledNum(this)" >
</div>
<div>
<label for="mark3">Mark Three</label>
<input type="text"  name="mark3" id="mark3" size="3" maxlength ="3" value="0" onblur="isFilledNum(this)">
</div>
<div>
<label for="mark4">Exam Mark</label>
<input type="text"  name="mark4" id="mark4" size="3" maxlength ="3" value="0" onblur="isFilledNum(this)">
</div>
Code:
function isFilledNum(number) {

	var number1 = parseInt(number.value)
	

if( number1< 0 || number1 > 100 || isNaN(number1) == true) {

var box = number.id;
var val = number.value;

alert ("You have checked box " + box + " whose value is " + val);
}
trancecommunity is offline   Reply With Quote
Old 02-18-2013, 10:37 PM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Use this:-


Code:
<div>
<label for="mark1">Mark One</label>
 <input type="text" name="mark1" id="mark1" size="3" maxlength ="3" value="0" onfocus = "this.value = '' "onkeyup="if(/\D/g.test(this.value) || (this.value>100)) {alert('Only integer numbers from 0-100 are valid in this box.  '); this.value = ''; this.focus()}" />
</div>

<div>
<label for="mark2">Mark Two</label>
<input type="text" name="mark2" id="mark2" size="3" maxlength ="3" value="0" onfocus = "this.value = '' "onkeyup="if(/\D/g.test(this.value) || (this.value>100)) {alert('Only integer numbers from 0-100 are valid in this box.  '); this.value = ''; this.focus()}" />
</div>

<div>
<label for="mark3">Mark Three</label>
<input type="text" name="mark3" id="mark3" size="3" maxlength ="3" value="0" onfocus = "this.value = '' "onkeyup="if(/\D/g.test(this.value) || (this.value>100)) {alert('Only integer numbers from 0-100 are valid in this box.  '); this.value = ''; this.focus()}" />
</div>

<div>
<label for="mark4">Exam Mark</label>
<input type="text" name="mark4" id="mark4" size="3" maxlength ="3" value="0" onfocus = "this.value = '' "onkeyup="if(/\D/g.test(this.value) || (this.value>100)) {alert('Only integer numbers from 0-100 are valid in this box.  '); this.value = ''; this.focus()}" />
</div>

The advantage of this approach is that the validity of the entry can vary with textbox, i.e. one box may have a limit of 100, another a limit of 20 etc.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-18-2013, 10:41 PM   PM User | #11
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
Great stuff. Cheers
trancecommunity is offline   Reply With Quote
Old 02-19-2013, 08:28 AM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Add to each input

Code:
onblur= "if(this.value == ''){this.value = 0}"
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-19-2013, 09:37 PM   PM User | #13
trancecommunity
New Coder

 
Join Date: Feb 2013
Posts: 35
Thanks: 8
Thanked 0 Times in 0 Posts
trancecommunity is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Add to each input

Code:
onblur= "if(this.value == ''){this.value = 0}"
With the rest of the code you gave?
trancecommunity is offline   Reply With Quote
Old 02-20-2013, 09:37 AM   PM User | #14
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by trancecommunity View Post
With the rest of the code you gave?
Yes, that's what I meant by "add to each input". To spell it out:-

Code:
<div>
<label for="mark1">Mark One</label>
 <input type="text" name="mark1" id="mark1" size="3" maxlength ="3" value="0" onfocus = "this.value = '' "onkeyup="if(/\D/g.test(this.value) || (this.value>100)) {alert('Only integer numbers from 0-100 are valid in this box.  '); this.value = ''; this.focus()}" onblur= "if(this.value == ''){this.value = 0}"/>
</div>
That means that the field must have a value in it whether one is entered by the user or not.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:18 PM.


Advertisement
Log in to turn off these ads.