How to use jQuery InputMask with HTML template

I have an HTML input box for phone numbers.

I use InputMaskto format the input 860000000in the following 8 600 00 000as input.

$(window).load(function()
{
   var phones = [{ "mask": "# ### ## ###"}, { "mask": "# ### ## ###"}];
    $('#phone').inputmask({ 
        mask: phones, 
        greedy: false, 
        definitions: { '#': { validator: "[0-9]", cardinality: 1}} });
});

I also use the HTML template to check if the number starts with 86, and only 9 digits, but with InputMaskit it stops working and cannot reach it in any way:

<label for="phone" class="first-col">Mobile No.:</label>
<input type="text" id="phone" placeholder="8 600 00 000" required pattern="(86)\d{7}" />

And CSS to add green borders, if valid:

input:required:valid {
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;
}

Do you have any ideas?

Exists JS Fiddle

+6
source share
3 answers

, , , , , , , , .

:

Regex, , (8 6)\d{2} \d{2} \d{3} :

<input type="text" id="phone" placeholder="8 600 00 000" required pattern="(8 6)\d{2} \d{2} \d{3}" />

Demo:

:

input:required:valid {
	box-shadow: 0 0 5px #5cd053;
	border-color: #28921f;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.1.62/jquery.inputmask.bundle.js"></script>
<script> 
$(window).load(function()
{
   var phones = [{ "mask": "# ### ## ###"}, { "mask": "# ### ## ###"}];
    $('#phone').inputmask({ 
        mask: phones, 
        greedy: false, 
        definitions: { '#': { validator: "[0-9]", cardinality: 1}} });
});
</script>
<label for="phone" class="first-col">Mobile No.:</label>
<input type="text" id="phone" placeholder="8 600 00 000" required pattern="(8 6)\d{2} \d{2} \d{3}" />
Hide result

Fiddle.

+6

RegEx.

data-inputmask. 9 , 8 6 . , , .

HTML :

<input id="phone" data-inputmask="'mask': '8 699 99 999'" />
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

8 699 99 999 , 86, .

JavaScript :

$('#phone').inputmask();

!

$(document).ready(function() {
  $('#phone').inputmask();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.1.62/jquery.inputmask.bundle.js"></script>
<input id="phone" data-inputmask="'mask': '8 699 99 999'" />
Hide result
+2

You must use the event keyupand regex, as shown below. No need to use libs

document.getElementById("phone").addEventListener("keyup", function(){
    // restart the match
    this.value = this.value.replace(/\s/g, "");
    // Assess the amount needed
    var v = this.value.match(/(\d)(\d{1,3})?(\d{1,2})?(\d+)?/);
    if(v){
      // Save the desired value depending on its existence
      v =  (v[1]?v[1]+(v[2]?" "+v[2]+(v[3]?" "+v[3]+(v[4]?" "+v[4]:""):""):""):"");
      // and yea!
      this.value = v;
    }
});
<input  type="text" id="phone" placeholder="8 600 00 000" required onkeypress='return event.charCode >= 48 && event.charCode <= 57 && this.value.length < 12' pattern="\b86" title="description" value="8 6"/>
Run codeHide result
0
source

Source: https://habr.com/ru/post/1017022/


All Articles