leave button disabled if textbox has a whitespace


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
 
Ad

Advertisements

A

Alexey Smirnov

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)
..........
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top