测试邮箱格式 html react
部分代码
// 初始化数据
constructor(options) {
super(options);
this.state = {
Mail: '',//邮箱
ValidateMail: {
validate: true,//是否正确
msgWrong: '请输入正确的邮箱格式'//错误提示信息
}
}
}
validateMail() {//验证邮箱
const reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$")
let { Mail } = this.state
let validateTemp=reg.test(Mail)
this.setState(preState => {
return Object.assign({}, preState, {
ValidateMail: Object.assign({}, preState.ValidateMail, {
validate: validateTemp
})
})
})
return validateTemp
}
<tr>
<td><label htmlFor="txtMail" >邮箱:</label></td>
<td><input
type="text"
id="txtMail"
name="Mail"
onChange={this.handlerChange}
value={this.state.Mail} />
</td>
</tr>
{
!this.state.ValidateMail.validate &&
<tr>
<td></td>
<td>
<span>{this.state.ValidateMail.msgWrong}</span>
</td>
</tr>
}