gpt4 book ai didi

JavaScript 名称验证不起作用

转载 作者:行者123 更新时间:2023-11-30 12:33:42 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于JavaScript 名称验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26793905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com