showing results for - "javascript masking if input matches patter"
Ariana
20 Apr 2016
1document.getElementById("phone").addEventListener("keyup", function(){
2    // restart the match
3    this.value = this.value.replace(/\s/g, "");
4    // Assess the amount needed
5    var v = this.value.match(/(\d)(\d{1,3})?(\d{1,2})?(\d+)?/);
6    if(v){
7      // Save the desired value depending on its existence
8      v =  (v[1]?v[1]+(v[2]?" "+v[2]+(v[3]?" "+v[3]+(v[4]?" "+v[4]:""):""):""):"");
9      // and yea!
10      this.value = v;
11    }
12});