The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Prevent code from running? (JavaScript)
Discuss Prevent code from running? (JavaScript) in the JavaScript Development forum on Dev Shed. Prevent code from running? (JavaScript) JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 20th, 2002, 03:33 PM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: Sweden
Posts: 45
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
|

January 20th, 2002, 04:32 PM
|
|
|
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?
|

January 20th, 2002, 04:43 PM
|
|
Contributing User
|
|
Join Date: Nov 2001
Posts: 765
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.
|

January 20th, 2002, 05:47 PM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: Sweden
Posts: 45
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.
|

January 20th, 2002, 06:28 PM
|
|
Contributing User
|
|
Join Date: Nov 2001
Posts: 765
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
|

January 21st, 2002, 07:20 AM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: Sweden
Posts: 45
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.
|

January 21st, 2002, 09:21 AM
|
|
Junior Member
|
|
Join Date: Jan 2002
Location: Argentina
Posts: 11
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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! 
|

January 21st, 2002, 09:33 AM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: Sweden
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 12
|
|
Thank you very much "elgatovolador". That's what I was looking for. I'll experiment a little bit with it. Thanks!
|

January 21st, 2002, 09:55 AM
|
|
Contributing User
|
|
Join Date: Oct 2001
Location: London
Posts: 69
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.
|

January 21st, 2002, 11:51 AM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: Sweden
Posts: 45
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
}
}
|

January 21st, 2002, 12:47 PM
|
|
Senior Member
|
|
Join Date: Nov 2001
Location: Boston Ma.
Posts: 1,529
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!
|

January 21st, 2002, 08:24 PM
|
|
Contributing User
|
|
Join Date: Nov 2001
Posts: 765
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....
|

January 22nd, 2002, 07:31 AM
|
|
Contributing User
|
|
Join Date: Oct 2001
Location: London
Posts: 69
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?
|

January 22nd, 2002, 08:03 AM
|
 |
Contributing User
|
|
Join Date: Oct 2001
Location: New Zealand
|
|
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.
|

January 22nd, 2002, 03:11 PM
|
|
Contributing User
|
|
Join Date: Nov 2001
Posts: 765
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 ***?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|