JavaScript名称验证无效

时间:2014-11-07 04:10:33

标签: javascript html html5 validation

我的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>

5 个答案:

答案 0 :(得分:3)

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

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

你忘了给元素赋予“id”属性。发布/提交表单时,“name”属性将用于获取服务器端的值。

答案 1 :(得分:0)

您尚未向您的元素提供ID输入要么提供ID,要么将方法更改为

  

的getElementsByTagName(&#34;的firstName&#34);

答案 2 :(得分:0)

添加 id="firstname" 以完美地完成工作,因为这在输入字段中不存在,因此无法找到 getElementById('firstname') 找不到此元素,< / p>

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

查看 jsFiddle

答案 3 :(得分:0)

可以肯定的是,养成在输入中添加姓名和ID的习惯

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

答案 4 :(得分:0)

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

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;