javascript - Issues with jQuery Masked plugin -


this field mask:

$('#cellphone').mask("(99)9999-9999?9", { placeholder: " " }); 
  1. i want clear mask when input lost focus. there way it? option of plugin...

    $('#cellphone').mask("(99)9999-9999?9", {      placeholder: " ",      clearonlostfocus: true  }); 

    or blur function:

    $('#cellphone').blur(function() {     // clear mask here. }); 
  2. i want change mask dynamically. i'm using function, , works pretty good...

    $('#cellphone').keyup(function () {     var newvalue = $(this).val()         .replace("(", "")         .replace(")", "")         .replace(" ", "")         .replace("-", "")         .replace("?", "");      if (newvalue.length > 10) {         $(this).mask("(99)9-9999-9999", { placeholder: " " });     } }); 

    but... when press backspace content of field selected, mask stops work. idea why happening?

thank you, guys!

the clearonlostfocus option works when have not filled mask correctly. i.e. if have not entered digits, leave input, clear it.

$('#cellphone').mask("(99)9999-9999?9", { placeholder: " ", clearonlostfocus: true }); 

if want input field blank when losing focus even when digits inside field match mask, need use blur event. in event callback, empty field, , re-apply input mask:

$("#cellphone").blur(function() {    $(this).val("");     $(this).mask("(99)9999-9999?9", { placeholder: " " }); }); 

the reason backspace or delete causes problems because plugin trying apply new mask line of text incorrect. in keyup function, need check if new mask in place (length of input field 15) , if key pressed backspace (code = 8) or delete key (code = 46). if so, need reapply old mask.

$('#cellphone').keyup(function (event) {     if ($(this).val().length > 14 && ((event.keycode == 8)||(event.keycode == 46))) {         $(this).mask("(99)9999-9999?9", { placeholder: " " });     } else {             var newvalue = $(this).val()                 .replace("(", "")                 .replace(")", "")                 .replace(" ", "")                 .replace("-", "")                 .replace("?", "");              if (newvalue.length > 10) {                 $(this).mask("(99)9-9999-9999", { placeholder: " " });             }     } }); 

jsfiddle here of above.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -