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 04-10-2010, 04:13 PM   PM User | #1
mitchellA
New to the CF scene

 
Join Date: Apr 2010
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
mitchellA is an unknown quantity at this point
I have 2 var & 2 function statements which are almost identical; one partially works

Hi there,
I've modified some code I found with a single variable & function to not allow certain keyboard input from a form (I eventually want this info to go into a cookie). I made two variables & functions; one which would not allow text, and one which would not allow numbers. The (new) text variable/function works fine, but the (modified) number variable/function only disallows the numbers 8 & 9. Please look at my code, try it (it doesn't work in IE8 or Firefox), and let me know how to fix it.
Thanks,
mitchellA
PS: this is my first posting and I'm not sure I've followed the rules properly.

Code:
<html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>Character Removal Test - by mitchellA</title>
<script language="javascript" type="text/javascript">
/* Visit http://www.yaldex.com/ for full source code
and get more free JavaScript, CSS and DHTML scripts! */
/* This script has been heavily modified from the original by mitchellA. */
<!-- Begin
var only_t = /[$\\@\\\#\%\^\&\*\(\)\[\]\+\ \{\}\`\~\=\|\/\?\.\,\<\>\'\"\!\:\;\0\1\2\3\4\5\6\7\8\9]/;
var only_n = /[$\\@\\\#\%\^\&\*\(\)\[\]\+\ \{\}\`\~\=\|\/\?\.\,\<\>\'\"\!\:\;\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z\A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z]/;

function chk_t(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar_1 = val.value.charAt((strLength) - 1);
if(lchar_1.search(only_t) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
   }
}
function chk_n(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar_2 = val.value.charAt((strLength) - 1);
if(lchar_2.search(only_n) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
   }
}
function chk2(form) {
if(form.value.length < 1) {
alert("Missing something.");
return false;
}
if((form.value.search(only_t) == -1) ||(form.value.search(only_n) == -1)) {
alert("Seems to be working.");
return false;
}
}
//  End -->
</script>
</head>
<form name=get_info onSubmit="return chk2(this.f_name); return chk2(this.l_name); return chk2(this.exp_hrs);">
<br>
<br>
First Name:
<input type="text" name="f_name" size="25" maxlength="15" value="" onKeyUp="javascript:chk_t(get_info.f_name);">
<br>
<br>
Last Name:
<input type="text" name="l_name" size="25" maxlength="20" value="" onKeyUp="javascript:chk_t(get_info.l_name);">
<br>
<br>
How many minutes do you want your cookie to last (10 - 60):
<input type="text" name="exp_mins" size="3" maxlength="2" value="" onKeyUp="javascript:chk_n(get_info.exp_mins);">
<br>
<br>
<input type=submit value=Continue>
<br>
</form>
<body>
	
</body>
</html>

Last edited by mitchellA; 04-11-2010 at 08:45 AM.. Reason: question answered well
mitchellA is offline   Reply With Quote
Old 04-10-2010, 05:31 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
OMG! Just to look at it, it ought to be obvious that this ghastly code is hopelessly inefficient and obsolete! Time to learn about regular expressions!

Try this:-


Code:
No numbers <input type = "text" id = "nonumbers" onblur = "nonums()"><br>
No text (numbers only) <input type = "text" id = "notext" onblur = "notext()"><br>

<script type = "text/javascript">
function nonums () {
var x = document.getElementById("nonumbers").value;
if (/[^a-z\s\-\']/gi.test(x)) {  // alpha chars, space, hyphen and apostrophe are allowed as text in proper name - Mary-Lou O'Reilly
alert ("You may not enter any numbers or special characters into this box!");
document.getElementById("nonumbers").value = "";  // clear the box
return false;
}
}

function notext() {
var x = document.getElementById("notext").value;
if (/[^0-9]/gi.test(x)) {
// if (/[^0-9\.]/gi.test(x)) {  // if decimal numbers allowed
alert ("You may enter only numbers into this box!");
document.getElementById("notext").value = "";
return false;
}
}

</script>
<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead. The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.


onKeyUp="javascript:chk_t(get_info.f_name);" "javascript:" is completely redundant. And prefer to use lower case for onkeyup (which is inappropriate here anyway).

As an alternative to function notext(), you may care to try this for the box where the user must enter a number from 10 to 60.

Code:
function TenToSixtyOnly () {
var x = parseInt(document.getElementById("exp_mins").value);
if (isNaN(x) || x < 10 || x >60) {  // isNaN means "is not a number"
alert ("You must enter a number from 10 to 60!");
document.getElementById("exp_mins").value = "";
return false;
}
}
Here is a more sophisticated alpha chars and space/hyphen/apostrophe script [you can use it to replace nonums() ] which works onkeypress, and defeats copying and pasting anything but letters in. I expect it is too advanced for you to understand fully so you may prefer to ignore it for the time being.

Code:
function isAlphaKey(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k ==45 || k ==39 || k ==32);
}
function extractAlpha(str) {
return str.replace(/[^a-z\s\.\-\']/gi,"");
}

</script>

Letters only <input type="text" onkeypress="return isAlphaKey(event)" onblur="this.value=extractAlpha(this.value)" /><br />
BTW, please help us to help you by following the posting guidelines and wrapping your code in CODE tags.
This means use the octothorpe or # button on the toolbar which will insert the tags. You can (and should) edit your previous post.



“I don't pretend we have all the answers. But the questions are certainly worth thinking about..” - Arthur C. Clarke quotes (English Writer of science fiction, b.1917

Last edited by Philip M; 04-10-2010 at 07:50 PM.. Reason: Noticed typo
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
mitchellA (04-11-2010)
Old 04-11-2010, 08:40 AM   PM User | #3
mitchellA
New to the CF scene

 
Join Date: Apr 2010
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
mitchellA is an unknown quantity at this point
Thank you for telling me how to properly enclose my script in the tags by using the # button. More importantly, thank you for information about outdated scripts and how to use modern scripting language to solve my problem. I especially liked the ascii code to only allow certain input.
This code is for 1/4 of a homework assignment, and my teacher does not really want it because he expects something that works if people enter info as asked; I expect people to try to thwart the programmer, and want to learn good programming techniques. This is a step in the right direction.
Thanks again,
mitchellA
mitchellA 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 02:43 PM.


Advertisement
Log in to turn off these ads.