<body>
<p>Enter your email:</p>
<input id="email" style="margin-bottom: 20px; margin-top: 2px;" type="email"
placeholder="Email: ">
<input onclick= "formSubmission(email)" type="submit"
value="Submit">
<script>
const formRecord = []
const formSubmission = (email) =>{
if (email.value){
if (email.value.indexOf('@')){
formRecord.push(`Email :` + email.value)
}
else{
alert(`Please enter the valid email!`)
}
}
else{
return alert(`Please fill the area of email!`)
}
document.write(formRecord)
}
</script>
</body>
我写了这个逻辑,用'@'来唯一化邮件,但输出不畅,请告诉我如何不用正则表达式来封装邮件。
为了让你的代码正常工作,你只需要调整一下这一行 if (email.value.indexOf('@')) {
到 if (email.value.indexOf('@') > -1) {
但对于这样的事情,RegExp真的是你最好的选择......。
const formRecord = []
const formSubmission = (email) => {
if (email.value) {
if (email.value.indexOf('@') > -1) {
formRecord.push(`Email :` + email.value)
} else {
alert(`Please enter the valid email!`)
}
} else {
return alert(`Please fill the area of email!`)
}
document.write(formRecord)
}
<body>
<p>Enter your email:</p>
<input id="email" style="margin-bottom: 20px; margin-top: 2px;" type="email" placeholder="Email: ">
<input onclick="formSubmission(email)" type="submit" value="Submit">
</body>