charlie
charlie

Reputation: 481

JQuery if value is in an array

i am trying to use this code:

        <?php
        $return_arr = array();
        $sql="SELECT * from customer where email <> '' group by email ";
        $rs=mysql_query($sql,$conn) or die(mysql_error());
        while($result=mysql_fetch_array($rs)) {
            $return_arr[] = array('value' => $result["email"]);
        }
        $data = json_encode($return_arr);
        ?>
        <script type="text/javascript">
        $(document).ready(function(){
            var email_array = <?php echo $data; ?>;

            $('#email').on('keyup',function(){
                if ($.inArray($(this).val(), email_array) !== -1) {
                    alert();
                }
            });
        });
        </script>

to check if a value already exists when typing in a text box

my input is: <input type="text" id="email" />

when i type an email in that exists in the JQuery array its not showing an alert.

The value of the JQuery array is like:

var email_array = [{"value":"[email protected]"},{"value":"[email protected]"}];

Upvotes: 0

Views: 81

Answers (3)

Arsalan Javed
Arsalan Javed

Reputation: 51

Checkout this enter code here DEMO this will check character by character in any array. if it helps endorse please.

Upvotes: 0

Cerlin
Cerlin

Reputation: 6722

replace

$return_arr[] = array('value' => $result["email"]);

with

$return_arr[] = $result["email"];

This will return array of strings instead of array of objects

Upvotes: 1

macl
macl

Reputation: 995

Change the code of the javascript function to:

if ($.inArray({"value":$(this).val()}, email_array) !== -1) {
        alert();
}

Upvotes: 0

Related Questions