Javascript or Jquery validation on textChanged

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>


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() {
            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";



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.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x