<div className="form-group">
<label className="col-sm-0 control-label"> Name : </label>
<input
type="text"
value={this.state.UserName}
onChange={this.handleChangeUserName}
placeholder="Name"
pattern="[A-Za-z]{3}"
className="form-control"
/>
</div>
您好,我正在尝试使用模式验证验证React中的表单输入字段。但它不起作用。我使用的验证就像pattern="[A-Za-z]{3}"
一样简单。
请让我知道如何解决这个问题。将验证放在React Bootstrap组件中。
答案 0 :(得分:2)
您正在使用input
元素的value属性(表示受控组件)并使用onChange
方法更新状态,因此您可以在onChange
中轻松测试此正则表达式并更新状态只有当输入有效时。
像这样:
handleChangeUserName(e){
if(e.target.value.match("^[a-zA-Z ]*$") != null){
this.setState({UserName: e.target.value});
}
}
检查工作代码:
class HelloWidget extends React.Component {
constructor(){
super();
this.state={UserName:''}
this.handleChangeUserName = this.handleChangeUserName.bind(this);
}
handleChangeUserName(e){
if(e.target.value.match("^[a-zA-Z ]*$")!=null) {
this.setState({UserName: e.target.value});
}
}
render(){
return(
<div className="form-group">
<label className="col-sm-0 control-label" htmlFor="textinput"> Name : </label>
<input type="text" value={this.state.UserName} onChange={this.handleChangeUserName} placeholder="Name" className="form-control"></input>
</div>
)
}
}
ReactDOM.render(<HelloWidget/>, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='container' />
检查jsfiddle
是否有工作示例:https://jsfiddle.net/uL4fj4qL/11/
如果用户尝试输入错误的值,请检查添加jsfiddle
,Material-Ui snackbar
以显示错误:https://jsfiddle.net/4zqwq1fj/
答案 1 :(得分:0)
pattern="[A-Za-z]{3}"
是HTML5中的功能。
此处完整的解决方案:https://codepen.io/tkrotoff/pen/qypXWZ?editors=0010
class FormValidate extends React.Component {
state = {
username: ''
};
handleUsernameChange = e => {
console.log('handleUsernameChange()');
this.setState({
username: e.target.value
});
}
handleSubmit = e => {
console.log('handleSubmit() Submit form with state:', this.state);
e.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="username">Name</label>
<input
id="username"
name="username"
value={this.state.username}
onChange={this.handleUsernameChange}
placeholder="Name"
pattern="[A-Za-z]{3}"
className="form-control" />
</div>
<button className="btn btn-primary">Submit</button>
</form>
);
}
}
class FormNoValidate extends React.Component {
state = {
username: '',
error: ''
};
handleUsernameChange = e => {
console.log('handleUsernameChange()');
const target = e.target;
this.setState({
username: target.value,
error: target.validationMessage
});
}
handleSubmit = e => {
console.log('handleSubmit() Submit form with state:', this.state);
e.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit} noValidate>
<div className="form-group">
<label htmlFor="username">Name</label>
<input
id="username"
name="username"
value={this.state.username}
onChange={this.handleUsernameChange}
placeholder="Name"
pattern="[A-Za-z]{3}"
className="form-control" />
<div className="invalid-feedback d-block">
{this.state.error}
</div>
</div>
<button className="btn btn-primary">Submit</button>
</form>
);
}
}
我编写了一个非常简单的React库来处理表单验证,并支持HTML5属性:https://github.com/tkrotoff/react-form-with-constraints
此处的示例:https://github.com/tkrotoff/react-form-with-constraints/blob/master/README.md#examples
答案 2 :(得分:0)
使用React钩子,我们可以构建自定义钩子,以使验证更加容易。与你的榜样打击。您可以通过以下方式轻松添加register
:react hook形式:https://github.com/bluebill1049/react-hook-form
import React from 'react';
import useForm from 'react-hook-form';
function Test() {
const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);
return {
<form className="form-group" onSubmit={handleSubmit(onSubmit)}>
<label className="col-sm-0 control-label"> Name : </label>
<input
type="text"
ref={register}
placeholder="Name"
pattern="[A-Za-z]{3}"
className="form-control"
/>
</div>
}
}