Link to home
Start Free TrialLog in
Avatar of Bobby X
Bobby XFlag for United States of America

asked on

Need help on hitting "Enter" key to trigger a button...

Hi,

I'm trying to make the "Enter" key function exactly the same as clicking on the Find button that has an ' OnClientClick="return validateForm()" ' and does a PostBack. In other words, if the user hits the "Enter" key on his keyboard after filling in the zip code, I need the "Enter" key to do exactly as clicking on the "Find" button.

Thanks very much in advance.

<script type="text/javascript" src="/js/jquery-1.2.1.js"></script>
    <script type="text/javascript">
        // <![CDATA[
        function validateForm() {
            var zipCode = $("input#<%= txtZipCode.ClientID %>").val();
            zipCode = $.trim(zipCode);
            var state = document.getElementById("<%= ddlState.ClientID %>").value;
            var objZip = document.getElementById("<%= txtZipCode.ClientID %>");
            if (zipCode == "" && state == "") {
                alert("Please enter a zip code or select a state!");
                objZip.focus();
                return false;
            }
            if (zipCode != "" && !validateZipCode(zipCode)) {
                alert("Please enter a valid zip code!");
                objZip.focus();
                return false;
            }
            
            return true;
        }
        
        function validateZipCode(v) {
            var regex = /^\d{5}$|^\d{5}-\d{4}$/;
            return regex.test(v);
        }
        // ]]>
    </script>		
 
    <div>
	<form id="form1" runat="server">
        <table border="0" cellpadding="2" style="width:500px">
            <tr id="trZip" runat="server">
                <td style="text-align:right">Please enter a zip code</td>
                <td style="text-align:left">
                    <asp:TextBox ID="txtZipCode" Width="40px" MaxLength="5" Runat="server" />
                </td>
            </tr>
            <tr id="trOR" runat="server">
                <td style="text-align:right">OR</td>
                <td>&nbsp;</td>
            </tr>
            <tr id="trState" runat="server">
                <td style="text-align:right;vertical-align:top">Select a state</td>
                <td style="text-align:left">
                    <asp:DropDownList 
                    ID="ddlState" 
                    DataSourceID="srcState" 
                    DataTextField="StateName" 
                    DataValueField="State_ID" 
                    AppendDataBoundItems="true" 
                    Runat="server">
                    <asp:ListItem Text="Select State" Value="" />
                    </asp:DropDownList>
                    <br /><br />
                    <asp:Button ID="btnFind" Text="Find" OnClick="btnFind_Click" OnClientClick="return validateForm()" Runat="server" />
                </td>
            </tr>
        </table>
		
        <asp:ObjectDataSource
            id="srcState"
            TypeName="State"
            SelectMethod="SelectStates"
            Runat="server" />
	</form>
    </div>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of drypz
drypz
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial