Sai*_*Sai 1 reactjs react-router reactjs-native
我正在研究'React.js'项目.我想创建3个文本字段,其中,第一个文本字段 - 我想只插入十六进制值.它应该接受0-9的数字和AF和冒号的字母.它应该只接受23个字符(数字,来自AF和冒号的字母).
第二个文本字段 - 它应该只有十六进制值.
第3个文本字段 - 它应仅接受字母数字值(仅限数字和
字母).
第4个文字字段 - 仅字母.
注意:不应接受特殊字符.
请帮我解决这个问题.
示例代码:
constructor(props) {
super(props);this.state = {showModal: true};
this.modalFooter = this.modalFooter.bind(this);
this.modalBody = this.modalBody.bind(this); this.updateState = this.updateState.bind(this);
};
modalFooter() {
return (
<div>
<BButton name="Cancel" onClickHandler={() => { this.setState({ showModal: false }) }} />
</div>);
}
modalBody() {
return (
<div>
<br className="Class">
<br> Hex Value: <input type="text" className="Class" formnovalidate="return isNumber(event)"
maxLength="23" placeholder="" /></br>
<br> Addr: <input type="text" className="Class" maxLength="6" name=""
placeholder="" /></br><br> Name: <input type="text" className="Class" id="Number"
maxLength="64"
name="" placeholder="" /></br>
</br>
</div>
);
}
updateState(e) {
this.setState({data: e.target.value});
}
render() {
let body = this.modalBody();
let footer = this.modalFooter();
let modal = <BModal header="Add Message"
body={body}
footer={footer} />
return (
<div className="page-title">
<center>
<h3> Sample Program </h3>
</center>
<hr className="horizontal-line"></hr>
<div> <font color="grey"><input type="text" value={this.state.data}
onClick={() => { this.setState({ showModal: true }) }} /></font>
{this.state.showModal ? modal : ""}
</div>
</div>);
}
Run Code Online (Sandbox Code Playgroud)
小智 12
我建议你使用React onKeyPress事件和regexp验证(参见下面的例子和jsbin链接)
var Form = React.createClass({
firstMethod(e) {
const re = /[0-9A-F:]+/g;
if (!re.test(e.key)) {
e.preventDefault();
}
},
secondMethod(e) {
const re = /[0-9A-F]+/g;
if (!re.test(e.key)) {
e.preventDefault();
}
},
thirdMethod(e) {
const re = /[0-9a-fA-F]+/g;
if (!re.test(e.key)) {
e.preventDefault();
}
},
fourthMethod(e) {
const re = /[a-fA-F]+/g;
if (!re.test(e.key)) {
e.preventDefault();
}
},
render() {
return (
<form>
<input ref="first" onKeyPress={(e) => this.firstMethod(e)} />
<input ref="second" onKeyPress={(e) => this.secondMethod(e)} />
<input ref="third" onKeyPress={(e) => this.thirdMethod(e)} />
<input ref="fourth" onKeyPress={(e) => this.fourthMethod(e)} />
</form>
);
}
});
ReactDOM.render(
<Form />,
document.getElementById('example')
);
Run Code Online (Sandbox Code Playgroud)
http://jsbin.com/juyakaqawe/edit?html,js,output