【问题标题】:JavaScript name validation not workingJavaScript 名称验证不起作用
【发布时间】:2015-01-03 19:28:43
【问题描述】:

我的 JavaScript 验证功能似乎不起作用。相同的验证格式似乎也不适用于我的其他输入,例如。姓。但是,它确实适用于我的电子邮件输入。我正在创建一个网页,在完整的代码中,我有这个 JavaScript 函数来验证名字,所以提交时该字段不为空。

function validateFName() {

    var firstname = document.getElementById('firstname').value;
    if( firstname==null || firstname=="")
    {
    alert("Please input a first name");
    }
 }

另外,下面是正文中的输入代码:

<label> First Name: </label> <input type="text" name="firstname"  maxlength="30" placeholder="John" /> <br><br>

下面是函数的调用:

<input type="submit" onclick="validateEmail(); validateFName(); validateLName(); validateGender(); validateDate(); validateVName(); validateVEmail();" /> <br>

以下是我的完整代码。 (尚未完成)

<!DOCTYPE html>
<html>
<head>

  <title>  Truth or Dare </title>


  <style type='text/css'>
    body {
  font: 15px 'Lucida Grande', Verdana, sans-serif;
  color: #404040;
  background: purple;
}

nav {
width: 360px;
height: 70px;
margin-left: auto;
margin-right: auto;
}

.fancyNav{
    /* Affects the UL element */
    overflow: hidden;
    display: inline-block;
}

.fancyNav li{
    /* Specifying a fallback color and we define CSS3 gradients for the major browsers: */

    background-color: #f0f0f0;
    background-image: -webkit-gradient(linear,left top, left bottom,from(#fefefe), color-stop(0.5,#f0f0f0), color-stop(0.51, #e6e6e6));
    background-image: -moz-linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);
    background-image: -o-linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);
    background-image: -ms-linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);
    background-image: linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);

    border-right: 1px solid rgba(9, 9, 9, 0.125);

    /* Adding a 1px inset highlight for a more polished efect: */

    box-shadow: 1px -1px 0 rgba(255, 255, 255, 0.6) inset;
    -moz-box-shadow: 1px -1px 0 rgba(255, 255, 255, 0.6) inset;
    -webkit-box-shadow: 1px -1px 0 rgba(255, 255, 255, 0.6) inset;

    position:relative;

    float: left;
    list-style: none;
}

.fancyNav li:after{

    /* This creates a pseudo element inslide each LI */ 

    content:'.';
    text-indent:-9999px;
    overflow:hidden;
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
    z-index:1;
    opacity:0;

    /* Gradients! */

    background-image:-webkit-gradient(linear, left top, right top, from(rgba(168,168,168,0.5)),color-stop(0.5,rgba(168,168,168,0)), to(rgba(168,168,168,0.5)));
    background-image:-moz-linear-gradient(left, rgba(168,168,168,0.5), rgba(168,168,168,0) 50%, rgba(168,168,168,0.5));
    background-image:-o-linear-gradient(left, rgba(168,168,168,0.5), rgba(168,168,168,0) 50%, rgba(168,168,168,0.5));
    background-image:-ms-linear-gradient(left, rgba(168,168,168,0.5), rgba(168,168,168,0) 50%, rgba(168,168,168,0.5));
    background-image:linear-gradient(left, rgba(168,168,168,0.5), rgba(168,168,168,0) 50%, rgba(168,168,168,0.5));

    /* Creating borders with box-shadow. Useful, as they don't affect the size of the element. */

    box-shadow:-1px 0 0 #a3a3a3,-2px 0 0 #fff,1px 0 0 #a3a3a3,2px 0 0 #fff;
    -moz-box-shadow:-1px 0 0 #a3a3a3,-2px 0 0 #fff,1px 0 0 #a3a3a3,2px 0 0 #fff;
    -webkit-box-shadow:-1px 0 0 #a3a3a3,-2px 0 0 #fff,1px 0 0 #a3a3a3,2px 0 0 #fff;

    /* This will create a smooth transition for the opacity property */

    -moz-transition:0.25s all;
    -webkit-transition:0.25s all;
    -o-transition:0.25s all;
    transition:0.25s all;
}

/* Treating the first LI and li:after elements separately */

.fancyNav li:first-child{
    border-radius: 4px 0 0 4px;
}

.fancyNav li:first-child:after,
.fancyNav li.selected:first-child:after{
    box-shadow:1px 0 0 #a3a3a3,2px 0 0 #fff;
    -moz-box-shadow:1px 0 0 #a3a3a3,2px 0 0 #fff;
    -webkit-box-shadow:1px 0 0 #a3a3a3,2px 0 0 #fff;

    border-radius:4px 0 0 4px;
}

.fancyNav li:last-child{
    border-radius: 0 4px 4px 0;
}

/* Treating the last LI and li:after elements separately */

.fancyNav li:last-child:after,
.fancyNav li.selected:last-child:after{
    box-shadow:-1px 0 0 #a3a3a3,-2px 0 0 #fff;
    -moz-box-shadow:-1px 0 0 #a3a3a3,-2px 0 0 #fff;
    -webkit-box-shadow:-1px 0 0 #a3a3a3,-2px 0 0 #fff;

    border-radius:0 4px 4px 0;
}

.fancyNav li:hover:after,
.fancyNav li.selected:after,
.fancyNav li:target:after{
    /* This property triggers the CSS3 transition */
    opacity:1;
}

.fancyNav:hover li.selected:after,
.fancyNav:hover li:target:after{
    /* Hides the targeted li when we are hovering on the UL */
    opacity:0;
}

.fancyNav li.selected:hover:after,
.fancyNav li:target:hover:after{
    opacity:1 !important;
}

/* Styling the anchor elements */

.fancyNav li a{
    color: #5d5d5d;
    display: inline-block;
    font: 20px/1 Lobster,Arial,sans-serif;
    padding: 12px 35px 14px;
    position: relative;
    text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.6);
    z-index:2;
    text-decoration:none !important;
    white-space:nowrap;
}

.fancyNav a.homeIcon{
    background:url('home.png') no-repeat center center;
    display: block;
    overflow: hidden;
    padding-left: 12px;
    padding-right: 12px;
    text-indent: -9999px;
    width: 16px;
}
h2{
    color: blue;
    font-size: 20px;
}
input {
  padding: 5px;
    font-size: 15px;
    text-shadow: 0px 1px 0px #fff;
    outline: none;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    border: 1px solid #ccc;
    -webkit-transition: .3s ease-in-out;
    -moz-transition: .3s ease-in-out;
    -o-transition: .3s ease-in-out;
}

    input:focus {
        border: 1px solid #fafafa;
        -webkit-box-shadow: 0px 0px 6px #007eff;
        -moz-box-shadow: 0px 0px 5px #007eff;
        box-shadow: 0px 0px 5px #007eff;
    }


label{

  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

#box {
  width: 430px; margin: 60px auto; padding: 60px 30px;
    background: white; border: 1px solid #e1e1e1;
    -moz-box-shadow: 0px 0px 8px #444;
    -webkit-box-shadow: 0px 0px 8px #444;
}


h1 {
    font-size: 20px; 
    color: #445668; 
    text-transform: uppercase;
    text-align: center; 
    margin: 0 0 35px 0; 
    text-shadow: 0px 1px 0px #f2f2f2;
}
  </style>



<script type='text/javascript'>

function validateEmail() {

    var email = document.getElementById('email').value;
    if( email==null || email=="")
    {
    alert("Please input an email address");
    }
 }

function validateFName() {

    var firstname = document.getElementById('firstname').value;
    if( firstname==null || firstname=="")
    {
    alert("Please input a first name");
    }
 }

    function validateLName() {

    var lastname = document.getElementById('lastname').value;
    if( lastname==null || lastname=="")
    {
    alert("Please input a last name");
    }
 }

        function validateGender() {

    var gender = document.getElementById('gender').value;
    if( gender==null || gender=="")
    {
    alert("Please select a gender");
    }
 }


        function validateDate() {

    var date = document.getElementById('date').value;
    if( date==null || date=="")
    {
    alert("Please select a date");
    }
 }


        function validateVName() {

    var vname = document.getElementById('vname').value;
    if( vname==null || vname=="")
    {
    alert("Please input a victim's name");
    }
 }

 function validateVEmail() {

    var vemail = document.getElementById('vemail').value;
    if( vemail==null || vemail=="")
    {
    alert("Please input a victim's email");
    }
 }

</script>


</head>
<body>
<div id="navi">
  <nav>
<ul class="fancyNav">
         <li id="home"><a href="#home" class="homeIcon">Home</a></li>
         <li id="news"><a href="#truth">TRUTH</a></li>
         <li id="about"><a href="#Dare">DARE</a></li>
     </ul>
  </nav>
  </div>

<div id="box">
<form id="truth">

<h1> Truth </h1>
<label> First Name: </label> <input type="text" name="firstname"  maxlength="30" placeholder="John" /> <br><br>
<label> Last Name: </label> <input type="text" name="lastname"  maxlength="30" placeholder="Doe" /> <br><br>
<label> Email:</label> <input type="text" id="email" /> <br><br>
<label> Male </label><input type="radio" name="gender" value="male"/>
<label> Female </label><input type="radio" name="gender" value="female"/> <br><br>
<label> Date to be performed: </label><input type="date" name="date"/><br><br>
<h2> Victim </h2>
<label> Name: </label>  <input type="text" name="vname"  maxlength="30" /><br><br>
 <label> Email:</label> <input type="text" id="vemail" /> <br><br>


    <h2> Please select a truth questions below </h2> <br>
<input type="radio" name="truth" value="q1"> Have you ever fallen and landed on your head? <br>

  <input type="radio"  name="truth" value="q2"> Have you ever return too much change? <br>

  <input type="radio" name="truth" value="q3"> Have you ever been admitted into the hospital? <br>

  <input type="radio" name="truth" value="q4"> Have you ever baked a cake? <br>

  <input type="radio" name="truth" value="q5"> Have you ever cheated on test? <br>

  <input type="radio" name="truth" value="q6"> Did you ever wish you were never born? <br>

  <input type="radio" name="truth" value="q7"> Did you ever hide from Sunday School? <br><br>

<input type="submit" onclick="validateEmail(); validateFName(); validateLName(); validateGender(); validateDate(); validateVName(); validateVEmail();" /> <br>
</form>
</div>

</body>


</html>

【问题讨论】:

  • 查看answer
  • 问题其实出在 id 属性上。
  • 唯一有id的元素是email和vemail

标签: javascript html validation


【解决方案1】:

当你说 document.getElementById("firstname") 时,你的 html 标签应该是这样的:

input type="text" id="firstname" name="firstname"

您忘记为元素赋予“id”属性。当您发布/提交表单时,“名称”属性将用于获取服务器端的值。

【讨论】:

    【解决方案2】:

    添加 id="firstname" 以完美执行工作,因为输入字段中不存在因此无法找到 getElementById('firstname') 找不到此元素,

    <label> First Name: </label> <input type="text" name="firstname"  maxlength="30" placeholder="John" id="firstname"/>
    

    查看jsFiddle

    【讨论】:

      【解决方案3】:

      请务必养成在输入中添加姓名和 ID 的习惯

      <input type='text' id='firstName' name='firstName'>
      

      【讨论】:

      • 请删除“如果您的按钮是提交...”部分,因为它不是真的...如果您在函数中添加return falsee.preventDefault,那么它将被考虑当然...
      • ...并在您更改后在此处发表评论,我很乐意删除我的反对票...
      【解决方案4】:

      由于您专门标记了 并根据您对 Javascript 代码的验证要求,我认为使用 HTML 5 表单验证功能对您来说会更好。因此,减少了 Javascript 编码的工作量。

      <!DOCTYPE html>
      <html>
      
      <head>
        <title>Truth or Dare</title>
        <style type='text/css'>
          body {
            font: 15px'Lucida Grande', Verdana, sans-serif;
            color: #404040;
            background: purple;
          }
          nav {
            width: 360px;
            height: 70px;
            margin-left: auto;
            margin-right: auto;
          }
          .fancyNav {
            /* Affects the UL element */
            overflow: hidden;
            display: inline-block;
          }
          .fancyNav li {
            /* Specifying a fallback color and we define CSS3 gradients for the major browsers: */
            background-color: #f0f0f0;
            background-image: -webkit-gradient(linear, left top, left bottom, from(#fefefe), color-stop(0.5, #f0f0f0), color-stop(0.51, #e6e6e6));
            background-image: -moz-linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);
            background-image: -o-linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);
            background-image: -ms-linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);
            background-image: linear-gradient(#fefefe 0%, #f0f0f0 50%, #e6e6e6 51%);
            border-right: 1px solid rgba(9, 9, 9, 0.125);
            /* Adding a 1px inset highlight for a more polished efect: */
            box-shadow: 1px -1px 0 rgba(255, 255, 255, 0.6) inset;
            -moz-box-shadow: 1px -1px 0 rgba(255, 255, 255, 0.6) inset;
            -webkit-box-shadow: 1px -1px 0 rgba(255, 255, 255, 0.6) inset;
            position: relative;
            float: left;
            list-style: none;
          }
          .fancyNav li:after {
            /* This creates a pseudo element inslide each LI */
            content: '.';
            text-indent: -9999px;
            overflow: hidden;
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            z-index: 1;
            opacity: 0;
            /* Gradients! */
            background-image: -webkit-gradient(linear, left top, right top, from(rgba(168, 168, 168, 0.5)), color-stop(0.5, rgba(168, 168, 168, 0)), to(rgba(168, 168, 168, 0.5)));
            background-image: -moz-linear-gradient(left, rgba(168, 168, 168, 0.5), rgba(168, 168, 168, 0) 50%, rgba(168, 168, 168, 0.5));
            background-image: -o-linear-gradient(left, rgba(168, 168, 168, 0.5), rgba(168, 168, 168, 0) 50%, rgba(168, 168, 168, 0.5));
            background-image: -ms-linear-gradient(left, rgba(168, 168, 168, 0.5), rgba(168, 168, 168, 0) 50%, rgba(168, 168, 168, 0.5));
            background-image: linear-gradient(left, rgba(168, 168, 168, 0.5), rgba(168, 168, 168, 0) 50%, rgba(168, 168, 168, 0.5));
            /* Creating borders with box-shadow. Useful, as they don't affect the size of the element. */
            box-shadow: -1px 0 0 #a3a3a3, -2px 0 0 #fff, 1px 0 0 #a3a3a3, 2px 0 0 #fff;
            -moz-box-shadow: -1px 0 0 #a3a3a3, -2px 0 0 #fff, 1px 0 0 #a3a3a3, 2px 0 0 #fff;
            -webkit-box-shadow: -1px 0 0 #a3a3a3, -2px 0 0 #fff, 1px 0 0 #a3a3a3, 2px 0 0 #fff;
            /* This will create a smooth transition for the opacity property */
            -moz-transition: 0.25s all;
            -webkit-transition: 0.25s all;
            -o-transition: 0.25s all;
            transition: 0.25s all;
          }
          /* Treating the first LI and li:after elements separately */
          .fancyNav li:first-child {
            border-radius: 4px 0 0 4px;
          }
          .fancyNav li:first-child:after,
          .fancyNav li.selected:first-child:after {
            box-shadow: 1px 0 0 #a3a3a3, 2px 0 0 #fff;
            -moz-box-shadow: 1px 0 0 #a3a3a3, 2px 0 0 #fff;
            -webkit-box-shadow: 1px 0 0 #a3a3a3, 2px 0 0 #fff;
            border-radius: 4px 0 0 4px;
          }
          .fancyNav li:last-child {
            border-radius: 0 4px 4px 0;
          }
          /* Treating the last LI and li:after elements separately */
          .fancyNav li:last-child:after,
          .fancyNav li.selected:last-child:after {
            box-shadow: -1px 0 0 #a3a3a3, -2px 0 0 #fff;
            -moz-box-shadow: -1px 0 0 #a3a3a3, -2px 0 0 #fff;
            -webkit-box-shadow: -1px 0 0 #a3a3a3, -2px 0 0 #fff;
            border-radius: 0 4px 4px 0;
          }
          .fancyNav li:hover:after,
          .fancyNav li.selected:after,
          .fancyNav li:target:after {
            /* This property triggers the CSS3 transition */
            opacity: 1;
          }
          .fancyNav:hover li.selected:after,
          .fancyNav:hover li:target:after {
            /* Hides the targeted li when we are hovering on the UL */
            opacity: 0;
          }
          .fancyNav li.selected:hover:after,
          .fancyNav li:target:hover:after {
            opacity: 1 !important;
          }
          /* Styling the anchor elements */
          .fancyNav li a {
            color: #5d5d5d;
            display: inline-block;
            font: 20px/1 Lobster, Arial, sans-serif;
            padding: 12px 35px 14px;
            position: relative;
            text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.6);
            z-index: 2;
            text-decoration: none !important;
            white-space: nowrap;
          }
          .fancyNav a.homeIcon {
            background: url('home.png') no-repeat center center;
            display: block;
            overflow: hidden;
            padding-left: 12px;
            padding-right: 12px;
            text-indent: -9999px;
            width: 16px;
          }
          h2 {
            color: blue;
            font-size: 20px;
          }
          input {
            padding: 5px;
            font-size: 15px;
            text-shadow: 0px 1px 0px #fff;
            outline: none;
            -webkit-border-radius: 3px;
            -moz-border-radius: 3px;
            border-radius: 3px;
            border: 1px solid #ccc;
            -webkit-transition: .3s ease-in-out;
            -moz-transition: .3s ease-in-out;
            -o-transition: .3s ease-in-out;
          }
          input:focus {
            border: 1px solid #fafafa;
            -webkit-box-shadow: 0px 0px 6px #007eff;
            -moz-box-shadow: 0px 0px 5px #007eff;
            box-shadow: 0px 0px 5px #007eff;
          }
          label {
            margin: 0;
            padding: 0;
            border: 0;
            font-size: 100%;
            font: inherit;
            vertical-align: baseline;
          }
          #box {
            width: 430px;
            margin: 60px auto;
            padding: 60px 30px;
            background: white;
            border: 1px solid #e1e1e1;
            -moz-box-shadow: 0px 0px 8px #444;
            -webkit-box-shadow: 0px 0px 8px #444;
          }
          h1 {
            font-size: 20px;
            color: #445668;
            text-transform: uppercase;
            text-align: center;
            margin: 0 0 35px 0;
            text-shadow: 0px 1px 0px #f2f2f2;
          }
        </style>
      </head>
      
      <body>
        <div id="navi">
          <nav>
            <ul class="fancyNav">
              <li id="home"><a href="#home" class="homeIcon">Home</a>
      
              </li>
              <li id="news"><a href="#truth">TRUTH</a>
      
              </li>
              <li id="about"><a href="#Dare">DARE</a>
      
              </li>
            </ul>
          </nav>
        </div>
        <div id="box">
          <form id="truth">
      
            <h1> Truth </h1>
      
            <label>First Name:</label>
            <input type="text" nam e="firstname" maxlength="30" placeholder="John" required="required" />
            <br>
            <br>
            <label>Last Name:</label>
            <input type="text" name="lastname" maxlength="30" placeholder="Doe" required="required" />
            <br>
            <br>
            <label>Email:</label>
            <input type="email" id="email" required="required" />
            <br>
            <br>
            <label>Male</label>
            <input type="radio" name="gender" value="male" required="required" />
            <label>Female</label>
            <input type="radio" name="gender" value="female" required="required" />
            <br>
            <br>
            <label>Date to be performed:</label>
            <input type="date" name="date" required="required" />
            <br>
            <br>
      
            <h2> Victim </h2>
      
            <label>Name:</label>
            <input type="text" name="vname" maxlength="30" required="required" />
            <br>
            <br>
            <label>Email:</label>
            <input type="email" id="vemail" required="required" />
            <br>
            <br>
            <h2> Please select a truth questions below </h2> 
            <br>
            <input type="radio" name="truth" value="q1">Have you ever fallen and landed on your head?
            <br>
            <input type="radio" name="truth" value="q2">Have you ever return too much change?
            <br>
            <input type="radio" name="truth" value="q3">Have you ever been admitted into the hospital?
            <br>
            <input type="radio" name="truth" value="q4">Have you ever baked a cake?
            <br>
            <input type="radio" name="truth" value="q5">Have you ever cheated on test?
            <br>
            <input type="radio" name="truth" value="q6">Did you ever wish you were never born?
            <br>
            <input type="radio" name="truth" value="q7">Did you ever hide from Sunday School?
            <br>
            <br>
            <input type="submit" />
            <br>
          </form>
        </div>
      </body>
      
      </html>

      【讨论】:

      • 我希望我能这样做,但我们需要使用 JavaScript 进行所有验证。
      【解决方案5】:

      你没有给你的元素输入 ID 要么给一个 ID 要么把方法改成

      getElementsByTagName("firstName");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-27
        • 2012-02-08
        • 2011-11-30
        • 2014-11-29
        • 2016-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多