要检查文本框中的值是否仅从自动填充值输入。

时间:2012-04-16 13:02:43

标签: javascript jquery json autocomplete dojo

我正在开发一个Web应用程序。对于输入值,我使用自动完成,从数据库中获取其数据。

$(document).ready(function hello(){
    var myVar1 = <%=request.getAttribute("variable1")%>
      $("input#assignedbyid").autocomplete({
    source: myVar1
      });
});

上面给出的示例为我提供了一组使用JDBCjson代码从数据库中获取的值。在任何时间点myvar1中的值看起来像这样::

["Kapil","Mayur","Abhinav","Chandan"]

作为自动完成值的来源。 我的html代码,input tag onfocus调用了function hello().

<div id="lets"><input dojoType="dijit.form.ValidationTextBox" id="assignedbyid" name="assignedbyname"  required="true" onfocus="hello();" onblur="hi();"></div>

现在,我希望进行验证检查,检查用户输入的值是否仅来自自动完成值("Kapil","Mayur","Abhinav","Chandan"),而不是任何其他值。我有这个代码,如果没有输入任何值,文本框会震动(onblur="hi();")。在其他部分我想要检查完成。

<script type="text/javascript"> 
 function periodical() {
            $('#lets').effect('shake', { times: 5 }, 200);
        };
          $(document).ready(function() {
            $('#lets').hide().css('display','').fadeIn(600);
                }); 
        function hi(){
            var dude = dojo.byId("assignedbyid").value;

            if(dude==""){
                periodical();}
            else{

            if(dude)
                myVar1

                alert("value entered");}
                };
</script>

我的其他部分暂时不完整。如何才能做到这一点 ?。谢谢 。

3 个答案:

答案 0 :(得分:1)

如果您使用的是jQuery,则可以使用$.inArray()方法:

if($.inArray(enteredValue, myArray) == false) { ... } // If enteredValue is not in myArray

如果你只是使用普通的Javascript,它也很简单:

if(myArray.indexOf(enteredValue) == -1) { ... } // If enteredValue is not in myArray

答案 1 :(得分:0)

var V = $("#myInput").val();
if (V=="Kapil"||V=="Mayur"||V=="Abhinav"||V=="Chandan") {
    //do something
}

var V = $("#myInput").val();
var myArray = ["Kapil","Mayur","Abhinav","Chandan"];

if ($.inArray(V, myArray)!=-1) {
    //do something
}

答案 2 :(得分:0)

将此示例代码用于自动填充。

示例代码..

    var url1 = sitepath+'opportunity/contact_autocomplete/';
        var related= '';
        $(function() {
            $( "#txtcontactid" ).autocomplete(
            {
                source:url1,
                select: function( event, ui ) 
                {
                    log( 
                         ui.item.id 
                      );
                }
            });
        });
相关问题