Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Mobile Number Format in Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 305
    Comment on it

    Hi All,

    We in very common scenario come across a scenario where we want user to just be able to enter Mobile Number Format. This small code piece will help in validating user to enter only Mobile number format.

    $(".Mobilenumbers").keypress(function (e) {
        var key;
        var isCtrl = false;
        var keychar;
        var reg;
        if (window.event) {
            key = e.keyCode;
            isCtrl = window.event.ctrlKey
        }
        else if (e.which) {
            key = e.which;
            isCtrl = e.ctrlKey;
        }
        if (isNaN(key)) return true;
        keychar = String.fromCharCode(key);
    
        // Comment: check for backspace or delete, or if Ctrl was pressed
        if (key == 8 || isCtrl) {
            return true;
        }
        var regex = new RegExp("^[0-9]|[+]|[-]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            var Indx = $(this).val().indexOf('.');
            if (Indx !== -1 && key == 46) {
                e.preventDefault();
            }
            else {
                return true;
            }
        }
        e.preventDefault();
        return false;
    });
    

    "Mobilenumbers" is the class given to the input field for Entering Mobile Number.

    Happy Coding......

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: