Hem*_*ari 3 javascript regex email-validation reactjs
我有一个注册表单,我想以简单快捷的方式验证电子邮件。
现在我正在使用长正则表达式进行电子邮件验证,如下所示。
import React, { Component } from 'react';
import TextField from 'material-ui/TextField';
const emailPattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
export default class Register extends Component {
constructor(props) {
super(props);
this.state = {
emailError: '',
email: ''
};
}
handleTouchTap = () => {
if(this.state.email == '' || this.state.email == null){
this.setState({
emailError: "Email cannot be empty"
});
}
else if (!emailPattern.test(this.state.email) && this.state.email.length > 0) {
this.setState({
emailError: "Enter a valid email"
});
}
}else{
let data = {};
data.email = this.state.email;
// this.props.registerUser(data);
}
};
changeEmail = (evt) => {
this.setState({
email: evt.target.value
});
}
render() {
return (
<div>
<div className="module row" style={{display: 'flex'}}>
<section className="section">
<h1>Create an account</h1>
<h3>It's free and always will be!</h3>
<imports.TextField errorText={this.state.emailError} floatingLabelText="Email" hintText="Enter your email" value={this.state.email} onChange={this.changeEmail} name="email" />
</section>
<section className="section">
<div className="feature-link-carousel-cell">
<a href="https://www.glassdoor.com/blog/do-race-gender-play-a-role-in-salary-negotiations/" data-ga-category="homepage" data-ga-lbl="marketing-whatsnew" data-ga-action="whats-new-click">
<img width="960" data-original="https://media.glassdoor.com/home/feature-link/reviews/iStock_81884597_MEDIUM.jpg" className="lazy lazy-loaded" src="https://media.glassdoor.com/home/feature-link/reviews/iStock_81884597_MEDIUM.jpg" alt="Do Race & Gender Play a Role in Salary Negotiations? A New Study Says Yes" style={{opacity: 1}}>
</img>
</a>
</div>
</section>
</div>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
在 React 中是否有任何最简单、最短的方法来验证电子邮件?
ES6有更短的方法吗?
Ami*_*iry 14
我在自己的项目中使用了这个简单的 util 函数:
function validateEmail (email) {
const regexp = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regexp.test(email);
}
Run Code Online (Sandbox Code Playgroud)
这是通过正则表达式完成的,true如果输入是这样构造的,它将返回:username@domain.com
| 归档时间: |
|
| 查看次数: |
8304 次 |
| 最近记录: |