Solved

Verify AutoComplete Selected

Posted on 2013-11-07
1
195 Views
Last Modified: 2013-11-12
I'm trying to prevent users from submitting a form without selecting something from my autocomplete. I don't want free text in my searchbox.

So far I can't get my code to work. what can I do to prevent

 $(document).ready(function () {
            $("#<%=txtSearch.ClientID%>").autocomplete("Search.ashx", {
                 extraParams: { "type": function () { return $("input:radio[name='toggle']:checked").val(); } },
                 width: 200,
                 formatItem: function (data, i, n, value) {
                     return "<img style = 'width:50px;height:50px' src='Images/doctypes/" + value.split(",")[1] + ".png'/> " + value.split(",")[0];
                 },
                 formatResult: function (data, value) {
                     return value.split(",")[0] + " # " + value.split(",")[2]
                 },
                 select: function (e, ui) {
                     alert(ui.item.value);
                 }
                 



            });

            $("#<%=txtSearch.ClientID%>").result(function (event, data, formatted) {
                if (data) {
                    $("#<%=hfSelected.ClientID%>").val("1");
                    $("#<%=tester.ClientID%>").html("Done");
                    //auto-complete matched

                }
                else {
                    $("#<%=hfSelected.ClientID%>").val("0");
                    $("#<%=tester.ClientID%>").html("Not Done");
                    //there was no match
                }
            });

            $("#<%=txtSearch.ClientID%>").blur(function () {
                $("#<%=txtSearch.ClientID%>").search(); //trigger result() on blur, even if autocomplete wasn't used
       
            });


         });

Open in new window

0
Comment
Question by:NickMalloy
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 400 total points
ID: 39632116
add this :

$("form").submit(function(evt) {
     if( $("#<%=txtSearch.ClientID%>").val().length == 0 ) {
            alert("please search a client");
            evt.preventDefault();
     }
})

Open in new window


 
$(document).ready(function () {

$("form").submit(function(evt) {
     if( $("#<%=txtSearch.ClientID%>").val().length == 0 ) {
            alert("please search a client");
            evt.preventDefault();
     }
})

            $("#<%=txtSearch.ClientID%>").autocomplete("Search.ashx", {
                 extraParams: { "type": function () { return $("input:radio[name='toggle']:checked").val(); } },
                 width: 200,
                 formatItem: function (data, i, n, value) {
                     return "<img style = 'width:50px;height:50px' src='Images/doctypes/" + value.split(",")[1] + ".png'/> " + value.split(",")[0];
                 },
                 formatResult: function (data, value) {
                     return value.split(",")[0] + " # " + value.split(",")[2]
                 },
                 select: function (e, ui) {
                     alert(ui.item.value);
                 }
                 



            });

            $("#<%=txtSearch.ClientID%>").result(function (event, data, formatted) {
                if (data) {
                    $("#<%=hfSelected.ClientID%>").val("1");
                    $("#<%=tester.ClientID%>").html("Done");
                    //auto-complete matched

                }
                else {
                    $("#<%=hfSelected.ClientID%>").val("0");
                    $("#<%=tester.ClientID%>").html("Not Done");
                    //there was no match
                }
            });

            $("#<%=txtSearch.ClientID%>").blur(function () {
                $("#<%=txtSearch.ClientID%>").search(); //trigger result() on blur, even if autocomplete wasn't used
       
            });


         });

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

821 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