jquery - How change this mask javascript function to avoid start with zero? -


please, can change mask function avoid user type 0 (zero) first number?

this mask function format input @ format:

(99) 9999-9999 or (99) 99999-9999 

once here in brazil celphone has 9 digits , normal phones 8 digits. city code (between parentheses) not start 0 (zero), otherwise mess phone number.

// telefone com 9 digitos            var spmaskbehavior = function (val) {   return val.replace(/\d/g, '').length === 11 ? '(00) 00000-0000' : '(00) 0000-00009'; }, spoptions = {   onkeypress: function(val, e, field, options) {       field.mask(spmaskbehavior.apply({}, arguments), options);     } };  $('#telefone').mask(spmaskbehavior, spoptions); 

here's the mask link

if first digit entered zero...

that easy check using simple if condition.
need check second character of value, first (.

if zero, remove it... and, optionnaly, notify user.

// telefone com 9 digitos             var spmaskbehavior = function (val) {    return val.replace(/\d/g, '').length === 11 ? '(00) 00000-0000' : '(00) 0000-00009';  },      spoptions = {        onkeypress: function(val, e, field, options) {          if(field.val()=="(0"){            console.log("no! no 0 begin.");            field.val(field.val().substr(0,1)); // removes 0          }          field.mask(spmaskbehavior.apply({}, arguments), options);        }      };    $('#telefone').mask(spmaskbehavior, spoptions);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>    <input id="telefone">


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -