[英]How to verify a text field with api value in react js.?
Iam trying to add a verification in my website i want to verify my phone number digits according to the phone number digit object coming from the api I hace tried multiple things but it didn't work i would be great if any one can help.我正在尝试在我的网站中添加验证我想根据来自 api 的电话号码 object 验证我的电话号码数字我已经尝试了多种方法,但它没有工作如果有人可以提供帮助,我会很棒。
<div className="col-8 ml-0 pl-0">
<TextInputComponent
label={
<IntlMessages id="profile.emailbox.field.mobilenumber" />
}
type="text"
placeholder="Mobilnummer"
value={this.state.number}
onChange={(e) => {
if (e.target.value != this.props.user.data.user.phone_number_digits ) {
<small className="text-danger my-0 py-0">{this.state.errorNumber}</small>
}else {
this.handleChange(e, "number");
}
}}
/>
</div>
What I can see is that you are checking the input value on change and not checking the size/length of the input value and checking it with the api results.我可以看到您正在检查更改时的输入值,而不是检查输入值的大小/长度并使用 api 结果进行检查。
Which will surely not give desired results.这肯定不会产生预期的结果。 So try checking the length of the input.所以尝试检查输入的长度。
You can also use some good libraries like - https://catamphetamine.gitlab.io/react-phone-number-input/ ?您还可以使用一些好的库,例如 - https://catamphetamine.gitlab.io/react-phone-number-input/ ?
Keep in mind that it's near impossible to expect the end-user to enter the correct phone number format (in contrast to your server or api).请记住,期望最终用户输入正确的电话号码格式(与您的服务器或 api 相比)几乎是不可能的。 So I would suggest that you remove all special characters to make the formatting uniform.因此,我建议您删除所有特殊字符以使格式统一。 You can always do your own formatting with REGEX later if needed.如果需要,您以后可以随时使用 REGEX 进行自己的格式化。
Also, a comparison operator in javascript uses three equal signs.此外,javascript 中的比较运算符使用三个等号。 Not equals is.== so this could also be contributing to your issue.不等于 is.== 所以这也可能导致您的问题。
onChange={(e) => {
let mobileNumber = ("" + e.target.value).replace(/\D/g, "");
let mobileDigits = ("" + this.props.user.data.user.phone_number_digits).replace(/\D/g, "");
if (mobileNumber !== mobileDigits ) {
<small className="text-danger my-0 py-0">{this.state.errorNumber}</small>
}else {
this.handleChange(e, "number");
}
Update Upon looking at your code once more, it appears that you're setting the value of the input field as empty.更新再次查看您的代码时,您似乎将输入字段的值设置为空。
value={this.state.number}
Meaning that you're assigning the state value for "number" to the input field, but never updating it with onChange.这意味着您将“数字”的 state 值分配给输入字段,但从不使用 onChange 更新它。 Inside of your !== condition you should update state with在您的 !== 条件内,您应该使用更新 state
this.setState(prevState =>{
return{
...prevState,
number: mobileNumber
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.