Javascript or Jquery validation on textChanged

Tags: , ,



I need to do the validation for the P.O box on text changed event. If the user types P.O Box in the address text box and also request for expedite shipping by checking a chec box then I need to show the warning message to the user saying that expedite shipping cannot be done to the P.O box address. For this, I have the following code on my aspx page:

<asp:TextBox ID="txtAddress" runat="server" style="text-transform:uppercase;" onChange="addchange();"  ></asp:TextBox>

<asp:CheckBox  ID="chkExpShipping"  Font-Bold="true" ForeColor="Red"   CssClass="test" Text="ExpediteShipping" runat="server" />

    <asp:Panel ID="pnlMessage" runat="server" Visible="false" >
        <div  class="ui-radio ui-btn" style="font-size:20px;">
            <span style="color:blue"><b> Warning: Express delivery cannot be done to the P.O Box address</b>  </span>
        </div>

    </asp:Panel>

if the user types P.O box address in the txtAddress and also checks the chkExpShipping check box then I want the pnlMessage to be visible and show the warning message. If the user changes the txtAddress content from P.O box to regular address then I want the warning to be hidden. In order to achieve this, I wrote this code:

<script type="text/javascript">
        function addchange() {
            debugger;
            var add = document.getElementById('txtAddress').value;
            var postalRGEX = /^?i)b(?:p(?:ost)?.?s*(?:[o0](?:ffice)?)?.?s*b(?:[o0]x)?|b[o0]x;
            var PostalResult = postalRGEX.test(add);
            if (PostalResult == true) {
                document.getElementByID('<%=pnlMessage.ClientID%>').style.display = "block";
            }
            else {
                    document.getElementByID('<%=pnlMessage.ClientID%>').style.display = "none";
            }

            }
       

    </script>

when I start typing in the txtAddress text box, no validation happens. The code does not even go the addChjange javascript function.

any help with this will be appreciated.

Answer

Part of the problem may be that you’re doing a comparison with == when JavaScript has much more predictable results when using the triple ===. Try: if (PostalResult === true)



Source: stackoverflow