Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Sweden
    Posts
    45
    Rep Power
    14

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19
    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?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    *Good point*. You should be filtering the input (using a select list, clickable links, menu) rather than trying to handle an invalid argument.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Sweden
    Posts
    45
    Rep Power
    14
    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.
    // Pikachu420
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Sweden
    Posts
    45
    Rep Power
    14
    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.
    // Pikachu420
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    Argentina
    Posts
    11
    Rep 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!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Sweden
    Posts
    45
    Rep Power
    14

    Thumbs up


    Thank you very much "elgatovolador". That's what I was looking for. I'll experiment a little bit with it. Thanks!
    // Pikachu420
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    London
    Posts
    69
    Rep Power
    13
    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Sweden
    Posts
    45
    Rep Power
    14
    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
      }
    }
    // Pikachu420
  20. #11
  21. No Profile Picture
    Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    Boston Ma.
    Posts
    1,529
    Rep 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!
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    Hmmm...

    1)
    You should be filtering the input...
    2)
    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)
    Why dont you just filter invalid user input...
    4)
    Thank you very much "elgatovolador". That's what I was looking for.


    Did I misread this?

    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....
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2001
    Location
    London
    Posts
    69
    Rep Power
    13
    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?
    Humble Seeker

    The longest journey starts with the smallest step, and knowledge is the longest journey of all.
  26. #14
  27. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2001
    Location
    New Zealand
    Posts
    1,774
    Rep Power
    24
    Netscape Navigator and IE4 (I think) had a javascript on error function (quoted from http://www.devguru.com/Technologies/...n_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.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    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***?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo