简体   繁体   English

javascript正则表达式允许数字,并且前三位数字后只能有一个空格

[英]javascript Regex to allow number and only one space after first three digits

I am working on Angular\\Ionic 3 project and I want a regex to validate the phone number that is enter in the Input box.Currently I am able restrict the user to enter only the number but I want 3 digits and then one space and then 7 digits... eg) 300 1234567 Below is my HTML code: 我正在Angular \\ Ionic 3项目上工作,我想要一个正则表达式来验证在``输入''框中输入的电话号码。当前,我可以限制用户只输入数字,但我希望输入3位数字然后一个空格然后7位数字...例如)300 1234567下面是我的HTML代码:

<ion-input type="tel" 
                  (input)="onKeyPress($event,MobileNoDisplay)" 
                   maxlength="11" 
                   placeholder="300 1122345" 
                   [value]="MobileNoDisplay"
                   [(ngModel)]="MobileNoDisplay">
        </ion-input>

OnkeyPress function code: OnkeyPress功能代码:

onKeyPress(event,val: string) {

    if (/[\D]/.test(val) ) { 
    this.MobileNoDisplay = val.replace(/[\D]+/g, '');//this will remove any non-
                                                     numerical value
     console.log("MobileNoDisplay-->" + this.MobileNoDisplay);
    }
    else{
      console.log("ELSE CHECK");      
    }

    event.stopPropagation();
  }

} }

This is admittedly not a pure-regexp solution you asked for but your code sample suggests it's not necessary: 公认这不是您所要求的纯正则表达式解决方案,但是您的代码示例表明这不是必需的:

// first check if the value does not match the pattern (see the exclamation mark)
if (!/^\d{3} \d{7}$/.test(val)) { 
    // first remove non-digits
    var numeric = val.replace(/\D/g, '');
    // get the first 3 chars, and if the filtered number is longer then 3, add a space, then add the remaining 7 chars, beginning on the fourth char (index 3)
    this.MobileNoDisplay = numeric.substr(0, 3)
        + (numeric.length > 3 ? ' ' + numeric.substr(3, 7) : '');
    console.log("MobileNoDisplay-->" + this.MobileNoDisplay);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 中的正则表达式允许最多 10 位数字,数字之间只有一个下划线,并防止在前 5 位数字后输入下划线 - Regex in JavaScript which allow maximum 10 digits, only one underscore in between the digits and prevent entering underscore after the first 5 digits 正则表达式只允许数字,第一个位置一个“$”和一个小数 - javascript - Regex allow only numeric, one '$' in first position and one decimal - javascript javascript中的正则表达式只允许数字和一个点后跟最多2个数字 - regex in javascript allow only numbers and one dot followed by max 2 number JavaScript中的小数点后仅允许两位数 - Allow only two digits after decimal in javascript JavaScript 正则表达式限制字符数并允许数字且不超过一个字母 - JavaScript regex to limit number of characters and allow digits and no more than one letter Javascript正则表达式最多只能匹配11位数字,一个逗号和2位数字 - Javascript regex to match only up to 11 digits, one comma, and 2 digits after it Javascript:仅允许输入 7 位数字,并且仅在第三个数字后自动添加连字符 (-) - Javascript: Only allow 7 digits for input and automatically add hyphen(-) after only 3rd number 一个正则表达式允许恰好 5 位数字,数字前后有一个可选的空格? - A regex to allow exactly 5 digit number with one optional white space before and after the number? JavaScript仅允许字母和数字 - JavaScript only allow letter space and number 如何在javascript中设置手机号码验证的前三位数? - How to Set The first three digits for mobile number varification in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM