Results 1 to 6 of 6

Thread: How to get the background color of a form field

  1. #1
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to get the background color of a form field

    I have built a spry widget form where the submit button is at the bottom of the page, and all my required fields are at the top of the page. The problem for a user is that when it is submitted and there is an error in a required field, it seems that nothing happens. Only by scrolling back up to the top of the page will they see the error field.
    What I would like to do is test for the background color of the field and if it is not the good field color, then have an alert tell them which field is invalid.
    The code I'm trying occurs on the submit operation.

    My form is named addForm. Here is what I have.
    <form method="POST" action="Admin/DataSave.html" name="addForm" onSubmit="return fieldErrors()">

    This is the function to run on the onSubmit.
    The span id for the field where you enter your name is sprytextfield1. If this field validates then the spry widget gives the field the background color of #B8F5B1.

    <script type="text/javascript">
    <!--
    function fieldErrors() {
    var theBgd = document.getElementById("sprytextfield1.style.backgroundColor");
    If(theBgd != "#B8F5B1")
    alert("Oops, there is an error with your name.");
    return false;
    }
    //-->
    </script>

    Where am I going wrong?
    I'm a newbee to scripting.
    Thanks for any help.
    Last edited by deweyhtucker; 07-04-2010 at 02:53 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Whats the ID of the field? sprytextfield1?
    You have to do something like this: element.style.backgroundColor
    So if the id is sprytextfield1, try:
    Code:
    var theBgd = document.getElementById("sprytextfield1").style.backgroundColor;
    Jeremy | jfein.net

  3. #3
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <style type="text/css">
    <!--
    #t2 {
      background-Color:#B8F5B1;
    }
    
    -->
    </style>
    <script type="text/javascript">
    <!--
    
    function fieldErrors(id,hex) {
     var fld=document.getElementById(id),theBgd = zxcRGBtoHex(zxcSV(fld,'background-Color'));
     if(theBgd != hex.toLowerCase()){
      alert("Oops, there is an error with your\n"+fld.title);
      return false;
     }
    }
    
    function zxcSV(obj,par){
     if (obj.currentStyle) return obj.currentStyle[par.replace(/-/g,'')];
     return document.defaultView.getComputedStyle(obj,null).getPropertyValue(par.toLowerCase());
    }
    
    
    function zxcRGBtoHex(zxcrgb){
    
     if (zxcrgb.charAt(0)=='#'){
      return zxcrgb.toLowerCase();
     }
     if (zxcrgb.charAt(0)=='r'){
      zxcrgb=zxcrgb.replace('rgb(','').replace(')','').split(',');
      return '#'+zxcHex(zxcrgb[0])+zxcHex(zxcrgb[1])+zxcHex(zxcrgb[2]);
     }
     return '';
    }
    
    function zxcHex(zxcnu) {
     if (zxcnu==null) return '00';
     zxcnu=parseInt(zxcnu);
     if (zxcnu==0||isNaN(zxcnu)) return '00';
     zxcnu=Math.max(0,Math.min(zxcnu,255));
     var zxchex='0123456789abcdef';
     return zxchex.charAt((zxcnu-zxcnu%16)/16)+zxchex.charAt(zxcnu%16);
    }
    
    //-->
    </script>
    </head>
    
    <body>
    <input title="First Name Field" id="t1" name="">
    <input title="Last Name Field" id="t2" name="">
    <script type="text/javascript">
    <!--
     fieldErrors('t1','#B8F5B1');
     fieldErrors('t2','#B8F5B1');
    //-->
    </script></body>
    
    </html>
    some browsers use hex some upper case some lower case some rgb for colors
    Last edited by vwphillips; 07-04-2010 at 07:30 PM.
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That will never work. When you ask the browser for the style.backgroundColor of anything, assuming it has one that was set by javascript or inline (background-color set in a stylesheet is unavailable with this method), the browser may respond with either the hex value (using upper or lower case letter where applicable) or the rgb(###, ###, ###) value. Other methods are available and may in some cases be used.

    Now your spry widget may set the color using javascript, or simply set it by adding/changing a class name. If the former, there is no guarantee that the browser will report the value in the same format as spry set it in (this actually varies by browser), if the latter, the value will be unavailable using the method under discussion here.

    A far better approach would be to use the spry widget's own code to determine which entry is invalid. It must already be doing this in order to make the input appear with a different background color. It probably even has an option to use this information as you see fit.

    Where did you get the code for this widget?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    That will never work.
    the code I posted accounts for hex,upper case,lower case and rgb

    and for inline and class name background color assignment
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by vwphillips View Post
    the code I posted accounts for hex,upper case,lower case and rgb

    and for inline and class name background color assignment
    Sorry Vic. We cross posted. I was still composing my post while you responded.

    My post was in response to Nile and deweyhtucker, not to you.

    I still think the most efficient way here would be to piggy back on the widget's own method for validating the inputs, thus bypassing any need to determine the background color. It's likely that the widget already has a hook for doing so. Even if it doesn't, it shouldn't be too hard to create one. But of course, like I said, we would need a link to the widget (or at least its code) to determine that. Preferably to its demo page, where hooks should be listed.

    Also, it's always possible, even though you appear to have all the bases covered, that a browser and/or the widget itself might throw you a curve on that.

    Nice coding on getting the computed/current style - most efficient approach I've seen on that yet.
    Last edited by jscheuer1; 07-04-2010 at 09:02 PM. Reason: add detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •