如何将邮件唯一化?

问题描述 投票:-1回答:1
<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>

我写了这个逻辑,用'@'来唯一化邮件,但输出不畅,请告诉我如何不用正则表达式来封装邮件。

javascript html function html-email indexof
1个回答
0
投票

为了让你的代码正常工作,你只需要调整一下这一行 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>
© www.soinside.com 2019 - 2024. All rights reserved.