【问题标题】:How to unique the email?如何唯一的电子邮件?
【发布时间】:2020-05-18 13:16:14
【问题描述】:
<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】:

要使您的代码正常工作,您只需将行 if (email.value.indexOf('@')) { 调整为 if (email.value.indexOf('@') &gt; -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>

【讨论】:

  • 好答案!谢谢!
  • @MianDanial 不客气...如果对您有帮助,请考虑将其标记为已接受的答案:)
猜你喜欢
  • 2015-03-02
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 2018-07-08
  • 1970-01-01
相关资源
最近更新 更多