filtering arrays with jQuery grep

Filtering Array using $.grep() method

Hello everyone, I am going to share the code sample for filter the array using $.grep() method in jQuery 

Table of Contents

I. HTML 5 for input textbox
II. jQuery code for filter mobile number on textbox keyup.

HTML 5 Code Sample


<input id="mobileNumber" required pattern="\d{10}" validationmessage="Invalid mobile number; must be ten digits">

jQuery Code Sample

<script type="text/javascript">
    var userPhoneList = [];
  
    $(function () {
        filterPhoneNumbers();
    });

    function filterPhoneNumbers() {
        $.getJSON("../API/Connect/GetUserPhones/@userSession.UserID")
            .done(function (data) {
                if (data !== undefined && data !== null) {
                    if (data.length > 0) {
                        $.each(data, function (key, val) {
                            userPhoneList.push(parseInt(val.MobileNo));
                        });
                    }
                }
            });
    }

    $('#mobileNumber').keyup(function () {
        var mobile = $(this).val();
        var result = $.grep(subscriberPhones, function (mPho, index) {
            return mPho === parseInt(mobile);
        });
        if (result.length > 0) {
            alert('Mobile number already exist.');
        }
    });
</script>


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.