【问题标题】:Check if string that must be just numbers has characters检查必须只是数字的字符串是否有字符
【发布时间】:2022-12-01 00:57:24
【问题描述】:

我有一个电话号码输入,它的类型不是数字,而是文本,我想检查输入是否有字符来验证它是否错误,我这样设置是因为我将格式输入到 123- 123-1234 这是我的输入

<input (keyup)="format()" (change)="format()" maxlength="12" inputmode="numeric" type='text' class="input" formControlName="celular" id="celular" name="celular">

这是我设置格式的 ts

  format(){
    $('#celular').val($('#celular').val().replace(/^(\d{3})(\d{3})(\d+)$/, "$1-$2-$3"));
  }

所以我想做的是知道我的输入值是否包含 aA-zZ 中的字符和一些不是的特殊字符 -

【问题讨论】:

    标签: javascript html jquery angular typescript


    【解决方案1】:

    在谷歌的帮助下,我发现了这个:

    Regex phoneRegex =
        new Regex(@"^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
    
    if (phoneRegex.IsMatch(subjectString)) {
        string formattedPhoneNumber =
            phoneRegex.Replace(subjectString, "($1) $2-$3");
    } else {
        // Invalid phone number
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-15
      • 2015-03-18
      • 2017-07-04
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2010-12-27
      相关资源
      最近更新 更多