regex js for phone validation
Question

Halo.. 

below are the script copy from somewhere.. it is only accept numeric.

How to add it accept special symbol and numeric         ,  (   ) + 1 2 3 4 5 6 7 8 9 0

============================


var input = document.querySelector('#Input_IdentificationNo3'),
    oldValue,
    regex = new RegExp(/^\d{0,20}$/g),
    mask = function(value) {
      var output = [];
        for(var i = 0; i < value.length; i++) {
          if(i !== 0 && i % 4 === 0) {
            output.push(" "); // add the separator
          }
          output.push(value[i]);
        }
        return output.join("");
    },
    unmask = function(value) {/^\d{0,20}$/g
      var output = value.replace(new RegExp(/[^\d]/, 'g'), ''); // Remove every non-digit character
      return output;
    },
    keydownHandler = function(e) {
      oldValue = e.target.value;
    },
    inputHandler = function(e) {
          var el = e.target,
               newValue = el.value
          ;
          newValue = unmask(newValue);
         
          if(newValue.match(regex)) {
            newValue = mask(newValue);
            el.value = newValue;
          } else {
            el.value = oldValue;
          }
    }
;

input.addEventListener('keydown', keydownHandler );
input.addEventListener('input', inputHandler );


Solution

jing tung wong wrote:

Halo.. 

below are the script copy from somewhere.. it is only accept numeric.

How to add it accept special symbol and numeric         ,  (   ) + 1 2 3 4 5 6 7 8 9 0

============================


var input = document.querySelector('#Input_IdentificationNo3'),
    oldValue,
    regex = new RegExp(/^\d{0,20}$/g),
    mask = function(value) {
      var output = [];
        for(var i = 0; i < value.length; i++) {
          if(i !== 0 && i % 4 === 0) {
            output.push(" "); // add the separator
          }
          output.push(value[i]);
        }
        return output.join("");
    },
    unmask = function(value) {/^\d{0,20}$/g
      var output = value.replace(new RegExp(/[^\d]/, 'g'), ''); // Remove every non-digit character
      return output;
    },
    keydownHandler = function(e) {
      oldValue = e.target.value;
    },
    inputHandler = function(e) {
          var el = e.target,
               newValue = el.value
          ;
          newValue = unmask(newValue);
         
          if(newValue.match(regex)) {
            newValue = mask(newValue);
            el.value = newValue;
          } else {
            el.value = oldValue;
          }
    }
;

input.addEventListener('keydown', keydownHandler );
input.addEventListener('input', inputHandler );


Hi Jing,


here is working code of jquery as per your requirement.


$("#btnValidate").click(function () {
                var isValid = false;
                var regex = /^[0-9-+()]*$/;
                isValid = regex.test($("#TextBox1").val());
                $("#spnError").css("display", !isValid ? "block" : "none");
                return isValid;
            });


jQuery.htm

Hello Jing,

I would suggest you take a look at this: https://www.w3resource.com/javascript/form/phone-no-validation.php

It's for PHP but certainly, you will be able to adapt.

Cheers!

Solution

jing tung wong wrote:

Halo.. 

below are the script copy from somewhere.. it is only accept numeric.

How to add it accept special symbol and numeric         ,  (   ) + 1 2 3 4 5 6 7 8 9 0

============================


var input = document.querySelector('#Input_IdentificationNo3'),
    oldValue,
    regex = new RegExp(/^\d{0,20}$/g),
    mask = function(value) {
      var output = [];
        for(var i = 0; i < value.length; i++) {
          if(i !== 0 && i % 4 === 0) {
            output.push(" "); // add the separator
          }
          output.push(value[i]);
        }
        return output.join("");
    },
    unmask = function(value) {/^\d{0,20}$/g
      var output = value.replace(new RegExp(/[^\d]/, 'g'), ''); // Remove every non-digit character
      return output;
    },
    keydownHandler = function(e) {
      oldValue = e.target.value;
    },
    inputHandler = function(e) {
          var el = e.target,
               newValue = el.value
          ;
          newValue = unmask(newValue);
         
          if(newValue.match(regex)) {
            newValue = mask(newValue);
            el.value = newValue;
          } else {
            el.value = oldValue;
          }
    }
;

input.addEventListener('keydown', keydownHandler );
input.addEventListener('input', inputHandler );


Hi Jing,


here is working code of jquery as per your requirement.


$("#btnValidate").click(function () {
                var isValid = false;
                var regex = /^[0-9-+()]*$/;
                isValid = regex.test($("#TextBox1").val());
                $("#spnError").css("display", !isValid ? "block" : "none");
                return isValid;
            });


jQuery.htm

Good to know you got solution

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.