如何验证文本的第一个字符是React Native中的特定数字
我正在尝试创建一个仅接受数字字符的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此正格:
/^3 \ d {9} $/
描述:
^
在行开始时断言位置3
必须是第一个字符必须是3。\ d {9}
9个字符,必须是数字之后的$
在您指定的第一个字符时,在行末端断言位置,9个下一个字符一个和行的开始/结束,总长度必须为10
。而不是线的末端
Use this regex :
/^3\d{9}$/
Description:
^
assert position at start of the line3
first character must be 3.\d{9}
9 characters after the first one has to be digits$
assert position at the end of a lineAs 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您可以使用下面的REGX来验证电话号码: -
您可以在此数组[3]中将数字放在您想要的电话号码开始。
You can use below Regx to validate phone number:-
You can put the numbers in this array [3] like [3789] that you want to the phone number starts with.