?
Solved

Verify AutoComplete Selected

Posted on 2013-11-07
1
Medium Priority
?
224 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 83

Accepted Solution

by:
leakim971 earned 1600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

831 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