【问题标题】:Instant validation with JavaScript - not sure where to start使用 JavaScript 进行即时验证 - 不知道从哪里开始
【发布时间】:2015-05-25 14:42:31
【问题描述】:

//PENDING
body {
  font-family: "Raleway", sans-serif;
  font-size: 14pt;
  /* 1em */
  color: white;
  text-align: center;
  background-image: url(./img/bg);
}
#cog {
  margin-top: 5%;
  margin-bottom: -5%;
}
section.hero {
  padding: none;
  margin-left: 10%;
  margin-right: 10%;
}
section.hero h1.heroText {
  font-size: 3em;
  font-weight: 100;
}
section.hero h1.subText {
  font-size: 1.5em;
  font-weight: 100;
}
div.footer {
  font-size: 1.75em;
  font-weight: 100;
  position: absolute;
  bottom: 5px;
  width: 100%;
  text-align: center;
}
input.nicerInput {
  height: 50px;
  width: 25%;
  outline: none;
  background-color: white;
  color: #aaa;
  font-size: 1em;
  text-align: center;
  font-family: "Raleway", sans-serif;
  font-weight: 100;
  display: inline-block;
}
input.submitButton {
  color: white;
  font-family: "Raleway", sans-serif;
  font-weight: 100;
  font-size: 1em;
  height: 56px;
  width: 150px;
  background-color: #5CCF58;
  text-decoration: none;
  text-align: center;
  outline: none;
  border: none;
  display: inline-block;
  position: relative;
  left: -6px;
  top: -1px;
  -webkit-transition: linear all 1s;
  -moz-transition: linear all 1s;
  -mos-transition: linear all 1s;
}
input.submitButton:hover {
  color: inherit;
  background-color: #28CD20;
  -webkit-transition: linear all 0.5s;
  -moz-transition: linear all 0.5s;
  -mos-transition: linear all 0.5s;
}
input.submitButton:active {
  color: inherit;
  background-color: #21B71C;
  -webkit-transition: linear all 0.1s;
  -moz-transition: linear all 0.1s;
  -mos-transition: linear all 0.1s;
}
.errorMsg {
  color: white;
  font-size: 1em;
  margin-left: 10%;
  margin-right: 10%;
  display: none;
}
.fa-spin-custom {
  -webkit-animation: spin 7500ms infinite linear;
  animation: spin 7500ms infinite linear;
}
@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(359deg);
    transform: rotate(359deg);
  }
}
@keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(359deg);
    transform: rotate(359deg);
  }
}
.fa-7x {
  font-size: 7.5em !important;
}
<!DOCTYPE HTML>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, user-scalable=no, width=device-width">
  <meta name="author" content="Olly John">
  <meta name="keywords" content="Stephen John Eyecare, Stephen John Opticians, Worcester, Opticians, Opticions">
  <link rel="stylesheet" type="text/css" href="./css/normalize.css">
  <link rel="stylesheet" type="text/css" href="./css/styles.css">
  <link href='http://fonts.googleapis.com/css?family=Raleway:400,100' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
  <title>Stephen John Eyecare - Coming Soon</title>
</head>

<body>
  <i class="fa fa-7x fa-cog fa-spin-custom" id="cog"></i>
  <section class="hero">
    <h1 class="heroText">We'll be coming <strong>really soon!</strong></h1>
    <h1 class="subText">If you'd like to be notified when we go live, please submit your e-mail address using the field below</h1>
  </section>
  <section class="field">
    <form action="" name="form" method="post">
      <div class="form">
        <input type="text" class="nicerInput" name="input" id="input" placeholder="you@youremail.com">
        <input type="submit" class="submitButton" disabled="true" value="submit" id="sub">
        <br>
        <br>
        <span class="errorMsg" id="empty">Please make sure to enter your e-mail address before submitting it to us. <br></span>
        <span class="errorMsg" id="format">The e-mail address you entered is invalid.  Please enter an e-mail address in the format johndoe@email.com</span>
      </div>
    </form>
  </section>
  <div class="footer">Stephen John Eyecare</div>
  <script src="bower_components/jquery/src/jquery.js"></script>
  <!-- Use jQuery to validate?-->
</body>

</html>

大家好,

我父亲要求我为他的网站制作一个登陆页面,他只是想在主机上放一些东西,这样就不会浪费了,因为他已经购买了空间和域名,但是有一个该网站的开发遇到了挫折,所以他只是要求我快速制作一些内容,说明主网站即将推出,并让人们能够注册以在它上线时收到通知。
我已经完成了所有前端的工作,并且我已经开始研究“表单”的验证方面,但我无法对其做出正面或反面,所以我希望有人可以提供一些关于我的见解如果你这么好心,就需要这样做。
到目前为止,页面的代码在 sn-p 中。

提前致谢

【问题讨论】:

  • 我对你的情况表示同情,但 SO 可能不是这种帮助的地方。你最好找一个 RL 朋友来帮助你,浏览一些关于构建 web 应用程序的教程,或者减少你的临时网站的范围(例如,只有一些文本、图像和一个 mailto 链接)。跨度>
  • 您究竟想要执行什么样的验证?就是那个问题。您应该考虑jQuery validation plugin,以免重新发明轮子;
  • 分享你的javascript。您在 blur 和 keyup 事件上使用了 validate(this.value)。也许我们可以帮助你。展示您迄今为止尝试过的内容
  • 我会使用 jQuery 验证插件
  • 我完全忘记了我放入的 onblur() 和 onKeyUp() 东西 - 我正在为他们学习教程,但它没有用,所以我摆脱了我的 JS 'd 为它写了,我想我只是忘了把它们拿出来.. 验证的想法是,如果该字段为空,则将显示第一条消息,提醒用户在该字段中放置一些东西。如果该字段已填写但不是有效的电子邮件地址,则会显示第二条消息,告诉用户他们输入的内容无效,如果他们输入的内容有效,按钮将被启用,他们可以将其发送出去。跨度>

标签: javascript jquery html css email-validation


【解决方案1】:

使用下面的代码可以进行验证。不要忘记放置主 jquery 并验证 min 文件

<!DOCTYPE HTML>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, user-scalable=no, width=device-width">
  <meta name="author" content="Olly John">
  <meta name="keywords" content="Stephen John Eyecare, Stephen John Opticians, Worcester, Opticians, Opticions">
  <link rel="stylesheet" type="text/css" href="./css/normalize.css">
  <link rel="stylesheet" type="text/css" href="./css/styles.css">
  <link href='http://fonts.googleapis.com/css?family=Raleway:400,100' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
  <title>Stephen John Eyecare - Coming Soon</title>
  <style>
  body {
  font-family: "Raleway", sans-serif;
  font-size: 14pt;
  /* 1em */
  color: #000;
  text-align: center;
  background-image: url(./img/bg);
}
#cog {
  margin-top: 5%;
  margin-bottom: -5%;
}
section.hero {
  padding: none;
  margin-left: 10%;
  margin-right: 10%;
}
section.hero h1.heroText {
  font-size: 3em;
  font-weight: 100;
}
section.hero h1.subText {
  font-size: 1.5em;
  font-weight: 100;
}
div.footer {
  font-size: 1.75em;
  font-weight: 100;
  position: absolute;
  bottom: 5px;
  width: 100%;
  text-align: center;
}
input.nicerInput {
  height: 50px;
  width: 25%;
  outline: none;
  background-color: white;
  color: #aaa;
  font-size: 1em;
  text-align: center;
  font-family: "Raleway", sans-serif;
  font-weight: 100;
  display: inline-block;
}
input.submitButton {
  color: white;
  font-family: "Raleway", sans-serif;
  font-weight: 100;
  font-size: 1em;
  height: 56px;
  width: 150px;
  background-color: #5CCF58;
  text-decoration: none;
  text-align: center;
  outline: none;
  border: none;
  display: inline-block;
  position: relative;
  left: -6px;
  top: -1px;
  -webkit-transition: linear all 1s;
  -moz-transition: linear all 1s;
  -mos-transition: linear all 1s;
}
input.submitButton:hover {
  color: inherit;
  background-color: #28CD20;
  -webkit-transition: linear all 0.5s;
  -moz-transition: linear all 0.5s;
  -mos-transition: linear all 0.5s;
}
input.submitButton:active {
  color: inherit;
  background-color: #21B71C;
  -webkit-transition: linear all 0.1s;
  -moz-transition: linear all 0.1s;
  -mos-transition: linear all 0.1s;
}
.errorMsg {
  color: white;
  font-size: 1em;
  margin-left: 10%;
  margin-right: 10%;
  display: none;
}
.fa-spin-custom {
  -webkit-animation: spin 7500ms infinite linear;
  animation: spin 7500ms infinite linear;
}
@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(359deg);
    transform: rotate(359deg);
  }
}
@keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(359deg);
    transform: rotate(359deg);
  }
}
.fa-7x {
  font-size: 7.5em !important;
}
label .error{color:red;}
  </style>
</head>

<body>
  <i class="fa fa-7x fa-cog fa-spin-custom" id="cog"></i>
  <section class="hero">
    <h1 class="heroText">We'll be coming <strong>really soon!</strong></h1>
    <h1 class="subText">If you'd like to be notified when we go live, please submit your e-mail address using the field below</h1>
  </section>
  <section class="field">
    <form action="" name="form"  id="sample_frm" method="post">
      <div class="form">
        <input type="text" class="nicerInput" name="user_email" id="input" placeholder="you@youremail.com">
        <input type="submit" class="submitButton" value="submit" id="sub">
         </div>
    </form>
  </section>
  <div class="footer">Stephen John Eyecare</div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
  
 <script>
  $( "#sample_frm" ).validate({
  rules: {
user_email: {
  required: true,
  email: true
}
  },
  messages:
  {
  user_email:{
  required:"Enter email id", //custom error message
  email: "valid email id"
  }
  }
});
</script>
  <!-- Use jQuery to validate?-->
</body>

</html>

【讨论】:

  • 谢谢,非常感谢您的帮助!我将如何处理错误消息?
  • 替换为脚本部分
  • 我的意思是我之前输入的错误消息,我原始代码中的两个跨度?还是那不可能?
  • 可能你需要用 CSS 写代码
  • 那会是什么?如果出现错误,只需将显示设置为内联而不是无?
猜你喜欢
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
相关资源
最近更新 更多