JSFiddle - React, Tailwind, and code Playground

by gabrieleromanato

HTML

<form action="#" method="post" id="form">
    <div>
        <input type="text" name="mobile" id="mobile" />
        <input type="submit" name="submit" id="submit" value="Send" />
    </div>
</form>

JavaScript

(function($) {
    $.fn.isMobileNumber = function(re) {
        var that = this;
        var value = this.val();

        try {

            var construct = re.constructor.toString();




        } catch (e) {

            return console.log('Missing or invalid argument.');


        }

        if (!that.is(':text')) {
            return console.log('Invalid form field.');

        }


        if (construct.indexOf('RegExp') == -1) {
            return console.log('Invalid regular expression');

        }



        return re.test(value);







    };
})(jQuery);



$('#form').submit(function(e) {
    var $re = /^3\d{9}$/;
    var valid = $('#mobile').isMobileNumber($re);


    if (!valid) {

        alert('This is not a valid Italian mobile number.');

        e.preventDefault();

    } else {

        alert('This is a valid Italian mobile number.');

        e.preventDefault();

    }



});