leave button disabled if textbox has a whitespace

  • Thread starter Thread starter hal
  • Start date Start date
H

hal

I have a web page using javascript to enable/disabled an image button
and switch its image when values are entered into a couple of
textboxes. I have all that working fine, but my code considers a
space a value. Anyone have any idea how to keep my button disabled if
spaces are entered into the textboxes? Below is my code:

<script type="text/javascript">

function SetImage()
{
var
txtAnswer1=document.getElementById("txtAnswer1").value;
var
txtAnswer2=document.getElementById("txtAnswer2").value;

if (txtAnswer1.length > 0 && txtAnswer2.length > 0)
{
var y=document.getElementById("btnSubmit");
y.src = "../Images/submit_active.gif";

window.document.getElementById("btnSubmit").disabled = false;
}

if (txtAnswer1.length == 0 || txtAnswer2.length == 0)
{
var y=document.getElementById("btnSubmit");
y.src = "../Images/submit_inactive.gif";

window.document.getElementById("btnSubmit").disabled = true;
}
}

</script>

<asp:TextBox ID="txtAnswer1" runat="server" MaxLength="255"
onKeyUp="SetImage()"></asp:TextBox>
<asp:TextBox ID="txtAnswer2" runat="server" MaxLength="255"
onKeyUp="SetImage()"></asp:TextBox>

Thanks
 
I have a web page using javascript to enable/disabled an image button
and switch its image when values are entered into a couple of
textboxes.  I have all that working fine, but my code considers a
space a value.  Anyone have any idea how to keep my button disabled if
spaces are entered into the textboxes?  Below is my code:

    <script type="text/javascript">

        function SetImage()
          {
            var
txtAnswer1=document.getElementById("txtAnswer1").value;
            var
txtAnswer2=document.getElementById("txtAnswer2").value;

            if (txtAnswer1.length > 0 && txtAnswer2.length > 0)
                    {
                        var y=document.getElementById("btnSubmit");
                        y.src = "../Images/submit_active.gif";

window.document.getElementById("btnSubmit").disabled = false;
                    }

            if (txtAnswer1.length == 0 || txtAnswer2.length == 0)
                    {
                        var y=document.getElementById("btnSubmit");
                        y.src = "../Images/submit_inactive.gif";

window.document.getElementById("btnSubmit").disabled = true;
                    }
          }

    </script>

<asp:TextBox ID="txtAnswer1" runat="server" MaxLength="255"
onKeyUp="SetImage()"></asp:TextBox>
<asp:TextBox ID="txtAnswer2" runat="server" MaxLength="255"
onKeyUp="SetImage()"></asp:TextBox>

Thanks

var txtAnswer1=document.getElementById("txtAnswer1").value;
var txtAnswer2=document.getElementById("txtAnswer2").value;

txtAnswer1 = txtAnswer1.replace(/^\s*|\s*$/g,"");
txtAnswer2 = txtAnswer2.replace(/^\s*|\s*$/g,"");

if (txtAnswer1.length > 0 && txtAnswer2.length > 0)
..........
 
Back
Top