reactjs - 带有电子邮件字段的组件,当用户单击按钮并且我需要验证电子邮件地址时,该字段将回传到服务器
问题描述
我有一个带有电子邮件输入的组件,我需要找到一种方法来检查电子邮件地址,以查看它是否像“name@email.com”一样有效,只要用户单击表单上的下一步按钮。我还需要将该输入的值与页面上的其他数据一起发回,我也不知道该怎么做。
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import {
Col, Row, Icon, Input, Tooltip
} from 'antd'
import Checkbox from '../elements/Checkbox'
import Markup from '../core/Markup'
const CustomerDetails = ({ customer }) => {
const { contact = {}, account = {}, site = {} } = customer || {}
const [disableInput, setDisableInput] = React.useState(false)
const [inputValue, setInputValue] = React.useState(contact.email)
function clearInput() {
setInputValue(' ')
}
function handleInputChange(event) {
setInputValue(event.target.value)
}
function CheckboxClick() {
if (!disableInput) {
clearInput()
}
setDisableInput(prevValue => !prevValue)
}
return (
<Container>
<h2>{account.name}</h2>
<Row>
<Col span={8}>
<H3>
<strong>Primary Contact:</strong>
</H3>
<P>{contact.name}</P>
<P>{contact.phone}</P>
</Col>
<Col span={8}>
<H3>
<strong>Service Address:</strong>
</H3>
<P>{site.address1}</P>
<P>{site.address2}</P>
<P>
{site.city}, {site.state}
{site.postalCode}
</P>
</Col>
<Col span={8}>
<H3>
<strong>Billing Address:</strong>
</H3>
<StyledMarkup>{account.billingStreet}</StyledMarkup>
<P>
{account.billingCity}, {account.billingState}
{account.billingPostalCode}
</P>
</Col>
</Row>
<br />
<Row>
<Col span={10}>
<h4>
PRIMARY CONTACT EMAIL
<Tooltip
placement="right"
title={primaryContact}
>
<StyledTooltipIcon
type="question-circle"
theme="filled"
/>
</Tooltip>
</h4>
</Col>
</Row>
<Row>
<Col span={8}>
<StyledInput
value={inputValue}
onChange={handleInputChange}
disabled={disableInput}
/>
</Col>
<Col span={2} />
<Col span={8}>
<StyledCheckbox
value={disableInput}
onChange={CheckboxClick}
/> EMAIL
OPT OUT{' '}
<Tooltip
placement="right"
title={emailText}
>
<StyledTooltipIcon
type="question-circle"
theme="filled"
/>
</Tooltip>
</Col>
</Row>
<br />
<Row>
<Col>
<StyledCheckbox /> EXEMPT
</Col>
</Row>
<br />
<Row>
<Col>
<H4>BUILDER</H4>
</Col>
</Row>
</Container>
)
}
CustomerDetails.propTypes = {
customer: PropTypes.object
}
CustomerDetails.defaultProps = {
customer: {}
}
const Container = styled.div`
text-align: left;
`
const StyledCheckbox = styled(Checkbox)`
&&& {
background: white;
input + span {
width: 35px;
height: 35px;
border: 2px solid ${({ theme }) => theme.colors.black};
}
input + span:after {
width: 12.5px;
height: 20px;
}
input:focus + span {
width: 35px;
height: 35px;
}
}
`
const StyledInput = styled(Input)`
max-width: 100%;
background: white;
&&& {
border: 2px solid ${({ theme }) => theme.colors.black};
border-radius: 0px;
height: 35px;
}
`
const ErrorContainer = styled.div`
span {
text-align: center;
}
`
const StyledTooltipIcon = styled(Icon)`
color: #1571da;
`
const H3 = styled.h3`
white-space: nowrap;
margin-top: 0;
margin-bottom: 0;
line-height: 1.5;
`
const H4 = styled.h4`
text-decoration: underline;
color: #1590ff;
`
const P = styled.p`
margin-top: 0;
margin-bottom: 0;
font-size: 1rem;
`
const StyledMarkup = styled(Markup)`
margin-top: 0;
margin-bottom: 0;
font-size: 1rem;
`
export default CustomerDetails
解决方案
推荐阅读
- javafx - 在没有包的 FXML 中使用自定义类
- php - PHP preg_match 问题
- ruby-on-rails - 如何使用 Amazon SES 设置 Ruby on Rails 操作邮箱?
- flask - Why does scrapy crawler only work once in flask app?
- javascript - 如何在 React 中显示图像,这些图像作为文件下载并保存在服务器上的文件夹中,文件路径存储在我的数据库中?
- raku - 如何声明一个接受类型化数组参数的函数
- python - Python循环等效Excel偏移等效?
- python - Nginx 单元不适用于 django(ModuleNotFoundError: No module named 'encodings')
- google-cloud-platform - 如果 url 映射设置为自定义,则 ssl 证书的 Google 负载平衡器问题
- java - SDK安装cmd错误:'source'不是内部或外部命令、可运行程序或批处理文件