简体   繁体   English

如何在反应js中验证具有api值的文本字段?

[英]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.

相关问题 如何在反应 js 中单击单选按钮时显示与 api 不同的值? - How to display different value from the api on radio button click in react js.? 如何在 react js 中使用 redux 在提交时设置 api 的响应。 - How to set response of an api on submit using redux in react js.? 如何在 React js 中访问 api 中的对象数组? - How to access array of objects from the api in react js.? 如何使用节点 js 仅返回 mongodb 中字段的值。 - How to return only value of a field in mongodb using node js. 如何在 React js 中使用 redux 来自 api 的 map 阵列响应。 - How to map array response coming from the api using redux in react js in react js.? 如何根据react js中的id依次显示一个api的数据? - How can i display data of an api one after another on the basis of id in react js.? 如何在 react js 中为来自 api 的动态表单字段设置一般 onchange。 - How to set a general onchange for the dynamic form fields coming from api in react js.? 如何在 react js 的表格网格中显示键值对? - How can i display key value pairs in a table grid in react js.? 检查文本字段中的值是否已更改并发布。 JS。 导轨 - Check if value in text filed was changed and post it. JS. Rails 如何在反应 js 中获得多个 select 选项? - How can i get multiple select options in react js.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM