Solved

Verify AutoComplete Selected

Posted on 2013-11-07
1
187 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now