View Full Version : What does the allow. object do?
skeatt
12-06-2002, 03:17 PM
I can't find what "allow." means in any book, it was used in a javascript example.
return (allow.indexOf(String.fromCharCode(k))!=-1);
http://www.codingforums.com/showthread.php?s=&threadid=10374
ahosang
12-06-2002, 03:25 PM
without looking at the thread, allow is a variable defined in the script somewhere. It is also a string variable.
Reason:
indexOf() is a function of string objects and that allow variable calls that function in the code you've shown.
whammy
12-06-2002, 11:39 PM
That was referring to the "Accept only letters in a field" post in the "Post a javascript" forum.
And ahosang is right - allow is a variable that Borgtex defined in the script. Since you asked how it works, here's a breakdown:
<head>
<script type="text/javascript">
<!--
var letters=' ABCÇDEFGHIJKLMNÑOPQRSTUVWXYZabcçdefghijklmnñopqrstuvwxyzàáÀÁéèÈÉíìÍÌïÏóòÓÒúùÚÙüÜ'
var numbers='1234567890'
var signs=',.:;@-''
var mathsigns='+-=()*/'
var custom='<>#$%&?¿'
function alpha(e,allow) { // allow is declared here! Its value depends on what is passed to the function (see the HTML below).
var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which); // this gets the key that was pressed on the keyboard!
return (allow.indexOf(String.fromCharCode(k))!=-1); // this returns a boolean (true or false) of whether the key pressed on the keyboard is in the var(s) above that correspond to allow!
}
// -->
</script>
</head>
<body>
<div>
<form id="example" action="java script://">
<input type="text" onkeypress="return alpha(event,numbers)" /> <!-- "event" is a javascript handler, which gets passed to "e" in the function "alpha()". The stuff after "event," is what gets passed to "allow" -->
<input type="text" onkeypress="return alpha(event,letters)" /> <!-- "event" is a javascript handler, which gets passed to "e" in the function "alpha()". The stuff after "event," is what gets passed to "allow" -->
<input type="text" onkeypress="return alpha(event,numbers+letters+signs)" /> <!-- "event" is a javascript handler, which gets passed to "e" in the function "alpha()". The stuff after "event," is what gets passed to "allow" -->
</form>
</div>
</body>
</html>
Get it?
A simpler example:
<script type="text/javascript">
<!--
function test(mystring){
alert(mystring);
}
test("Yay!");
// -->
</script>
P.S. The action="javascript://" in the <form> tag basically does absolutely nothing - it's just in there as a placeholder since the "action" attribute of a form tag is required in XHTML.
:D
skeatt
12-07-2002, 03:36 AM
:thumbsup: Thankyou, I get it!
for my own task I'm going to see if I can rewrite it to do the same thing in a beginners code (meaning longer form) so it breaks things down so I can make what's happening sink in.
skeatt
12-07-2002, 10:04 AM
Here's what I come up with, it taught me what was needed and what each bit did.
I took out the parseInt and it still worked, so why have it if it works without it..:confused:
function testThese(eventPassed,theseChar) {
var keyToTest = document.all? eventPassed.keyCode : eventPassed.which; // check for browser compatability.
var charPos = theseChar.indexOf(String.fromCharCode(keyToTest)) // is this keyToTest in theseCharacter strings.
return result = (charPos > -1)? true : false; // if it isn't it will result in -1
}
I discovered you can't take out the eventPassed and just make it
var keyToTest = document.all? event .keyCode : event .which;
IE5 still works but NN won't if I did that.
skeatt
12-07-2002, 01:22 PM
I found a bug in the script, mine and the other mentioned on Post a Javascript.
You can't backspace in NN (I'm using NN4.7), IE is OK,
Fixed it in mine by including....
return result = (charPos > -1 || keyToTest == 8 )? true : false;
Which is the backspace number for NN
whammy
12-07-2002, 10:44 PM
My original script worked with all netscape versions I tested, I believe he took out the || k = 8 part in the "improvement". :)
And actually, this works for me in all versions of netscape I tested (including 4.7!):
document.all ? k = e.keyCode : k = e.which;
did you by chance discover that by looking at the original post?
skeatt
12-08-2002, 09:13 AM
Originally posted by whammy
did you by chance discover that by looking at the original post?
Whammy, to be honest I didn't even think of looking, :o I searched the forum for backspace and found the == 8 in another topic.
I guess if your original verson had a comment what the k==8 was It might have clicked when I first read it.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.