立即截断前导零

我正在使用 jquery 脚本来屏蔽输入字段。这是一个电话号码。 https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html


我想像这样屏蔽输入字段:+32 123 45 67 89


但在比利时,人们习惯于填写他们的电话号码,如:0123 45 67 89。目标是立即截断前导零......


  $('.phone').mask('+32000 00 00 00', {placeholder: "Phone number"});

});


炎炎设计
浏览 125回答 2
2回答

www说

使用translation选项$('.phone').mask('+32Z00 00 00 00', {&nbsp; placeholder: "Phone number",&nbsp; translation: {&nbsp; &nbsp; 'Z': { pattern: /[1-9]/ }&nbsp; },&nbsp; onInvalid: function(val, e, f, invalid, options) {&nbsp; &nbsp; if(val === '+32') {&nbsp; &nbsp; &nbsp; event.target.value = '';&nbsp; &nbsp; }&nbsp; }});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js" integrity="sha256-Kg2zTcFO9LXOc7IwcBx1YeUBJmekycsnTsq2RuFHSZU=" crossorigin="anonymous"></script><input class="phone" />

慕运维8079593

我的第一个方法就是这个。退出空格,然后退出前导零(如果存在)。function truncateLeadingZero(phoneNumber) {&nbsp; &nbsp; var noWhiteSpaces = phoneNumber.split(' ').join('');&nbsp; &nbsp; while (noWhiteSpaces.length > 9 && noWhiteSpaces[0] === '0') {&nbsp; &nbsp; &nbsp; &nbsp; noWhiteSpaces = noWhiteSpaces.substring(1,noWhiteSpaces.length);&nbsp; &nbsp; }&nbsp; &nbsp; return noWhiteSpaces;}var phoneNumber = '0123 45 67 89';phoneNumber = truncateLeadingZero(phoneNumber)console.log(phoneNumber);我认为使用您在问题中发布的内容,如果您使用我的输出,电话号码将根据需要被屏蔽。它期望的是一个包含 9 个数字的字符串(掩码的每个零)。$('.phone').mask('+32000 00 00 00', {placeholder: "Phone number"});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript