JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 20th, 2002, 03:33 PM
Pikachu420 Pikachu420 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Sweden
Posts: 45 Pikachu420 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Prevent code from running? (JavaScript)

I've got an onBlur event on a textfield that changes the background color of a <DIV> to the value in the textfield. But when a user enters something that is not a valid value as a background color, an error message appears. How do I suppress this error message?
__________________
// Pikachu420

Reply With Quote
  #2  
Old January 20th, 2002, 04:32 PM
degsy degsy is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Posts: 1,882 degsy User rank is Sergeant (500 - 2000 Reputation Level)degsy User rank is Sergeant (500 - 2000 Reputation Level)degsy User rank is Sergeant (500 - 2000 Reputation Level)degsy User rank is Sergeant (500 - 2000 Reputation Level)degsy User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 21 h 19 m 30 sec
Reputation Power: 18
Quote:
when a user enters something that is not a valid value as a background color


How do users know what is valid and what is not?

Reply With Quote
  #3  
Old January 20th, 2002, 04:43 PM
mrrichardfeder mrrichardfeder is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2001
Posts: 765 mrrichardfeder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 45 sec
Reputation Power: 12
*Good point*. You should be filtering the input (using a select list, clickable links, menu) rather than trying to handle an invalid argument.

Reply With Quote
  #4  
Old January 20th, 2002, 05:47 PM
Pikachu420 Pikachu420 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Sweden
Posts: 45 Pikachu420 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
To give the users more freedom, I've chosen to use a textfield where they can type in the exact hexadecimal value of the color they want. For users who are not familiar with hexadecimal color codes there is a color picker consisting of a popup window with a full chart of the 216 (don't remember the exact number) web safe colors. When they click a color, the popup closes and the hexadecimal value is automatically entered into the textfield. I just wanted advice on how to suppress the error messages, not advice on how to create a user friendly site.

I see now that the subject of this thread is a little off, sorry about that.

Reply With Quote
  #5  
Old January 20th, 2002, 06:28 PM
mrrichardfeder mrrichardfeder is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2001
Posts: 765 mrrichardfeder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 45 sec
Reputation Power: 12
Quote:
I just wanted advice on how to suppress the error messages, not advice on how to create a user friendly site.


The best way to "suppress the error messages" is to filter input so there aren't any. Maybe you should filter responses to your posts to suppress unwanted advice.

_rf

Reply With Quote
  #6  
Old January 21st, 2002, 07:20 AM
Pikachu420 Pikachu420 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Sweden
Posts: 45 Pikachu420 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Or maybe people who don't know the answer to what I'm asking should refrain from posting? It's not that hard of a concept really. I ask a question and if I'm lucky, someone will answer it. I don't expect people who obviously don't know the answer to take up room in "my" thread with their bullsh i t "answers" and "holier than thou" attitudes.

Reply With Quote
  #7  
Old January 21st, 2002, 09:21 AM
elgatovolador elgatovolador is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: Argentina
Posts: 11 elgatovolador User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question

Why dont you just filter invalid user input whith an onkeypress event instead of the onBlur event?
Note that you've got to trap the onkeypress event fot Netscape with an extra line.
fe:
Code:
<script language="Javascript">
<!--
if (document.layers) window.captureEvents(Event.KEYPRESS);
document.onkeypress = document_onkeypress;
function document_onkeypress(e) {
	if (document.layers) key = e.which;
	else key = event.keyCode;
	//check for valid values an length...
	//if everything is correct, change the color
}
//-->
</script>


Hope it helps!

Reply With Quote
  #8  
Old January 21st, 2002, 09:33 AM
Pikachu420 Pikachu420 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Sweden
Posts: 45 Pikachu420 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Thumbs up

Thank you very much "elgatovolador". That's what I was looking for. I'll experiment a little bit with it. Thanks!

Reply With Quote
  #9  
Old January 21st, 2002, 09:55 AM
Humble Seeker Humble Seeker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: London
Posts: 69 Humble Seeker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 54 sec
Reputation Power: 12
A hex ref colour is just a number why not set a function to check the content of the textarea before changing colour of the background. Check:
a) That the value is a hex value
b) That the hex value is within the hex colour values.

That should avoid the error message your getting.
__________________
Humble Seeker

The longest journey starts with the smallest step, and knowledge is the longest journey of all.

Reply With Quote
  #10  
Old January 21st, 2002, 11:51 AM
Pikachu420 Pikachu420 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Sweden
Posts: 45 Pikachu420 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
Yeah, if I only knew how to do that. I'm using a slightly modified version of Dreamweaver's "MM_chageProp" script. The only check I've managed to work in is the length check (>5). How do I check that the value is a valid color code? Is there a mathematical formula or do I need to check against an array with all the codes in it or something?

Code:
<!--
function changeProp(objName,theProp,theValue) { //v3.0
  if (theValue.length > 5) {
    var obj = MM_findObj(objName);
    if (obj && (theProp.indexOf("style.")==-1 || obj.style)) eval("obj."+theProp+"='"+theValue+"'");
    onError = null
  }
}

Reply With Quote
  #11  
Old January 21st, 2002, 12:47 PM
Fataqui Fataqui is offline
Senior Member
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: Boston Ma.
Posts: 1,529 Fataqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 42 sec
Reputation Power: 0
Hi..........


try this..............


REMOVE this below and put your function there instead.................

Just copy the code below and put it in a text file, rename whatever.html, to see what it does

// PUT YOUR FUNCTION YOU WANT TO RUN IN HERE

alert("YOU HAVE CHOSEN A REAL COLOR");
return(false);


Code:


<SCRIPT>

	function check_color()
	{
		var frm = document.color;

		// REMOVE BLANK CHARACTERS FROM BOTH ENDS OF INPUT STRING

		frm.cuser.value = remove_end_blanks(frm.cuser.value);

		// CHECK FOR AND VALIDATE HEX INPUT
		var hex = validate_input(frm.cuser.value);

		// IF INPUT VALIDATED THEN CONTINUE WITH WHATEVER
		if (hex.length)
			{
                // PUT YOUR  FUNCTION YOU WANT TO RUN IN HERE

                     alert("YOU HAVE CHOSEN A REAL COLOR");
                     return(false);
			}
		// IF INPUT NOT VALIDATED DISPLAY INPUT ERROR MESSAGE AND CLEAR THE FORM
		     else
			{
			alert("Invalid RGB Color Input Value");
			clear_frm(frm, true);
			}

		// RETURN FOCUS BACK TO INPUT FIELD

		return(false);
	}


	function remove_end_blanks(scolor)
	{
		// REMOVE BLANK CHARACTERS FROM LEFT END OF STRING
		while(scolor.length && scolor.charAt(0) == ' ')
			scolor = scolor.substring(1, scolor.length);

		// REMOVE BLANK CHARACTERS FROM RIGHT END OF STRING
		while(scolor.length && scolor.charAt(scolor.length-1) == ' ')
			scolor = scolor.substring(0, scolor.length-1);

		return(scolor);
	}


	// VALIDATE HEXADECIMAL INPUT ROUTINES

	function validate_input(scolor)
	{
		var hex = "";

		scolor = scolor.toUpperCase();

		if (scolor.substring(0,1) == "#")
			scolor = scolor.substring(1,scolor.length);

		if (scolor.length == 6 && check_hex_chars(scolor) == true)
		   hex = scolor;

		return(hex);
	}


	function check_hex_chars(scolor)
	{
		for (var pos=0; pos<scolor.length; pos++)
			{
			var ch = scolor.charAt(pos);

			if ((ch < '0' || ch > '9') && (ch < 'A' || ch > 'F'))
				return(false);
			}

		return(true);
	}

	function clear_frm(frm, remove_color)
	{

		if (remove_color == true)
		frm.cuser.value = "";
		frm.cuser.focus();
	}


</script>

<FORM name=color>
<input type=text name=cuser value="" size=6 maxlength=6 onBlur="check_color()";>
</form>




F!

Reply With Quote
  #12  
Old January 21st, 2002, 08:24 PM
mrrichardfeder mrrichardfeder is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2001
Posts: 765 mrrichardfeder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 45 sec
Reputation Power: 12
Hmmm...

1)
Quote:
You should be filtering the input...


2)
Quote:
I don't expect people who obviously don't know the answer to take up room in "my" thread with their bullsh i t "answers"


3)
Quote:
Why dont you just filter invalid user input...


4)
Quote:
Thank you very much "elgatovolador". That's what I was looking for.




Did I misread this?

Quote:
I just wanted advice on how to suppress the error messages, not advice on how to create a user friendly site.


In other words, tell me how to suppress the messages, not how to avoid them in the first place, like you stupid programmers do. Otherwise, sod off!

Don't call others ***holes, you're the expert....

Reply With Quote
  #13  
Old January 22nd, 2002, 07:31 AM
Humble Seeker Humble Seeker is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2001
Location: London
Posts: 69 Humble Seeker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 54 sec
Reputation Power: 12
mrrichardfeder only a stupid programmer would suppress an error message instead of dealing with it. Using code to ensure that an error never occurs is great. I take it your not a programmer?

Reply With Quote
  #14  
Old January 22nd, 2002, 08:03 AM
binky's Avatar
binky binky is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2001
Location: New Zealand
Posts: 1,774 binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level)binky User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 58 m 15 sec
Reputation Power: 23
Netscape Navigator and IE4 (I think) had a javascript on error function (quoted from http://www.devguru.com/Technologies...an_onerror.html ):

window.onerror=myErrorHandler

function myErrorHandler() {
alert('A customized error message')
return true
}

But in IE5.5 it didn't work, and I had found the little git that was causing the problem so no need for it. Might be what you're after.

Reply With Quote
  #15  
Old January 22nd, 2002, 03:11 PM
mrrichardfeder mrrichardfeder is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2001
Posts: 765 mrrichardfeder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 45 sec
Reputation Power: 12
Quote:
mrrichardfeder only a stupid programmer would suppress an error message instead of dealing with it. Using code to ensure that an error never occurs is great. I take it your not a programmer?


Is this the ***Twilight Zone***?

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Prevent code from running? (JavaScript)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap