如何验证文本的第一个字符是React Native中的特定数字

发布于 2025-02-09 17:21:54 字数 544 浏览 1 评论 0原文

我正在尝试创建一个仅接受数字字符的textinput,第一个字符必须在React Native中为3。

            const [inputs, setInputs] = React.useState({
                num: '',
            });           

            let numOnly = /^[0-9\b]+$/;
            let valid = true;
    
            if (!inputs.num.trim() || inputs.num.length < 10 || !inputs.num.match(numOnly)) {//if number field is not in correct format
                handleError('Please input phone number correctly', 'phone');
                valid = false;
            }

I'm trying to create a textinput that only accepts numeric characters and the first character must be 3 in React Native.

            const [inputs, setInputs] = React.useState({
                num: '',
            });           

            let numOnly = /^[0-9\b]+$/;
            let valid = true;
    
            if (!inputs.num.trim() || inputs.num.length < 10 || !inputs.num.match(numOnly)) {//if number field is not in correct format
                handleError('Please input phone number correctly', 'phone');
                valid = false;
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2025-02-16 17:21:55

使用此正格:/^3 \ d {9} $/

描述:

  1. ^在行开始时断言位置
  2. 3必须是第一个字符必须是3。
  3. \ d {9} 9个字符,必须是数字之后的
  4. $在您指定的第一个字符时,在行末端断言位置

,9个下一个字符一个和行的开始/结束,总长度必须为10

。而不是线的末端

Use this regex : /^3\d{9}$/

Description:

  1. ^ assert position at start of the line
  2. 3 first character must be 3.
  3. \d{9} 9 characters after the first one has to be digits
  4. $ assert position at the end of a line

As you specified the first character, the 9 next ones and start/end of the line, the total length has to be 10.

You can replace $ by \Z for end of the string instead of end of the line

病女 2025-02-16 17:21:55

您可以使用下面的REGX来验证电话号码: -

const reg = /^[0]?[3]\d{9}$/;
if (reg.test(phone) === true) {
  if (phone.length > 9) {
    console.log("valid phone number");
  } else {
    console.log("Invalid phone number");
  }
}

您可以在此数组[3]中将数字放在您想要的电话号码开始。

You can use below Regx to validate phone number:-

const reg = /^[0]?[3]\d{9}$/;
if (reg.test(phone) === true) {
  if (phone.length > 9) {
    console.log("valid phone number");
  } else {
    console.log("Invalid phone number");
  }
}

You can put the numbers in this array [3] like [3789] that you want to the phone number starts with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文