Solved

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

Posted on 2009-07-15
1
350 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:WebAppDeveloper
1 Comment
 
LVL 13

Accepted Solution

by:
drypz earned 500 total points
ID: 24865699
Hi! Do this is in your html form like this

<form id="form1" runat="server" defaultbutton="btnFind">
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Class object 2 27
VB.Net How to Exit Sub - Exit Form??? 5 51
Help with C#, MVC, razor. 6 23
Adding  DYMO Labelprinter to c# client application 4 8
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now