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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book