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-06-2012, 03:06 PM   PM User | #1
Bobb4
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Bobb4 is an unknown quantity at this point
Need help explaining a piece of code

Hi, below is some code I've found at w3schools site. It demonstrates the use of "onkeypress" event by allowing only non-numerical input. It works great and is mostly clear to me, except for the part I marked in red. Could someone please explain this part to me step by step? It's probably a noobish question, but I thought you could only use "return" inside a function. Are "return true" and "return false" just optional parameters to the "onkeypress" event? If so, where else can they be used? Also what information exactly does the "(event)" being passed into the function "noNumbers()" carry?

<html>
<body>
<script type="text/javascript">
function noNumbers(e)
{
var keynum;
var keychar;
var numcheck;

if(window.event) // IE8 and earlier
{
keynum = e.keyCode;
}
else if(e.which) // IE9/Firefox/Chrome/Opera/Safari
{
keynum = e.which;
}
keychar = String.fromCharCode(keynum);
numcheck = /\d/;
return !numcheck.test(keychar);
}
</script>

<form>
Type some text (numbers not allowed):
<input type="text" onkeypress="return noNumbers(event)" /></form>

Last edited by Bobb4; 02-06-2012 at 03:27 PM..
Bobb4 is offline   Reply With Quote
Old 02-06-2012, 03:33 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,881
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
that is the oldest way of defining events (it dates back to pre-DOM times, hence sometimes called DOM-0 events). the return value (and therefore you need an explicit return statement) of an event attribute determines, whether the default action (in this case inserting a character) is executed or not.

the same code for DOM event handling (without taking pre IE9 into consideration)
PHP Code:
<input type="text" id="some_field"
Code:
function checkInput(evt)
{
    if (!(/\d/.test(evt.which))) {
        evt.preventDefault();
    }
}
document.getElementById("some_field", checkInput, true);
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
Bobb4 (02-06-2012)
Old 02-06-2012, 03:38 PM   PM User | #3
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
I don't really see your problem, but you can test it with an alert, so:-

Code:
numcheck = /\d/;
var x = !numcheck.test(keychar);
alert (x);
return x;
}
The return statement specifies the value to be returned by a function or event handler and performs the act of returning that value to where the function was called from.

The value returned is either true or false, depending on the evaluation of the regular expression numcheck = /\d/; and then !numcheck.test(keychar); This could have been simplified to var x = (!/\d/.test(keychar)); Meaning - test the value of keychar for the presence of a digit (true or false) - and reverse the result false/true. If the result is false (a number was found) then the normal action of the keypress is suppressed or cancelled.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
__________________

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-06-2012 at 03:42 PM..
Philip M is offline   Reply With Quote
Old 02-06-2012, 03:44 PM   PM User | #4
Bobb4
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Bobb4 is an unknown quantity at this point
Thank you, that was very helpful!
Bobb4 is offline   Reply With Quote
Reply

Bookmarks

Tags
event, javascript, onkeypress, return, syntax

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 03:33 AM.


Advertisement
Log in to turn off these ads.