【问题标题】:Problem with color changing text verification on form code表单代码上的颜色更改文本验证问题
【发布时间】:2019-11-14 20:35:40
【问题描述】:

我一直在尝试开发一个接收电子邮件和密码的 for,并通过查看用户是否在提交中包含“@”来验证电子邮件是否是电子邮件(目前)。假设,如果没有“@”,那么包含文本框“电子邮件”标题的段落标签应该改变颜色。不幸的是,事实并非如此,我不知道为什么会这样。我尝试了几种不同的方法来改变颜色,但都失败了。如果有人可以帮助我解决这个问题,那会让我很开心。

我已经尝试过使用不同的验证方法和不同的方法来改变段落标签的样式。例如,我尝试使用单独的样式代码,当没有找到“@”符号时,该段落应该从默认样式代码切换两个。我还尝试使用 js 代码运行的不同时间,例如 onSubmit 或 onBlur。这些更改并没有解决我的问题。

     html, body { height: 100%; }
       body {
         background:radial-gradient(ellipse at center, 
         rgba(255,254,234,1) 0%, rgba(255,254,234,1) 35%, #ffffff 100%);
         overflow: hidden;
         }

         .ocean { 
            height: 5%;
            width:100%;
            position:absolute;
            bottom:0;
            left:0;
            background: #ffffff;
          }

         .wave {
            background: url(https://s3-us-west- 
           2.amazonaws.com/s.cdpn.io/85486/wave.svg) repeat-x; 
           position: absolute;
           top: -198px;
           width: 6400px;
           height: 198px;
           animation: wave 7s cubic-bezier( 0.36, 0.45, 0.63, 0.53) 
           infinite;
           transform: translate3d(0, 0, 0);
          }

        .wave:nth-of-type(2) {
          top: -175px;
          animation: wave 7s cubic-bezier( 0.36, 0.45, 0.63, 0.53) 
          -.125s infinite, swell 7s ease -1.25s infinite;
          opacity: 1;
         }

        @keyframes wave {
         0% {
           margin-left: 0;
            }
         100% {
           margin-left: -1600px;
            }
           }

        @keyframes swell {
        0%, 100% {
           transform: translate3d(0,-25px,0);
         }
        50% {
         transform: translate3d(0,5px,0);
            }
           }

         .loginbox
        {
         width: 340px;
         height: 360px;
         background: #000;
         color: #fff;
         top: 50%;
         left:50%;
         position: absolute;
         transform: translate(-50%, -50%);
         box-sizing: border-box;
       }
      h2
       {
         margin: 0;
         padding: 0 0 20px;
         text-align: center;
         font-size: 22px; 
       }



      .loginbox input
      {
       width: 100%;
       margin-bottom: 20px;
      }

      .loginbox input[type="text"], input[type="password"]
      {
        border: none;
 
        border-bottom: 1px solid #fff;
        background: transparent;
        outline: none;
        height: 40px;
        color: #fff;
        font-size: 16px;
       }

       .loginbox input[type="submit"]
      {
        border: none;
        outline: none;
        height: 48px;
        background: #fb2525;
        color: #fff;
        font-size: 18px;
        border-radius: 20px;
       } 

       .loginbox input[type="submit"]:hover
       {
         cursor: pointer;
         background: #ffc107;
         color: #000;
       }

       .loginbox a
       {
        text-decoration: none;
        font-size: 12px;
        line-height: 20px;
        color: darkgrey;
       }

      .loginbox p
       {
         margin: 0;
         padding: 10px;
         font-weight: bold;
        }
        .loginbox a:hover
        {
          color: #ffc107;
         }
    <!DOCTYPE html>
     <html>

      <head>
	    <title>
		   Sign Up Page
	    </title>
	     <link rel="stylesheet" href="style.css" media="screen"/>
      </head>

      <body>
	
	     <script>
	    	function validateForm() {
		      var x = document.forms["myForm"]["fname"].value;
		      if (x == "") {
		      alert("something must be filled out");
		      return false;
		      }
		      else if (!(x.includes("@"))) 
		     {
			   alert("");
			   document.getElementById("nameT").className="error";
		       return false;
		     }
	     	}
	     </script>

          <div class="ocean">
          <div class="wave"></div>
          <div class="wave"></div>
          </div>
          <h1>
             <center>
                Whale
             </center>
          </h1>
          <div class="loginbox">

            <h2>
              Login
            </h2>
            <form name="myForm" onsubmit="return validateForm()" 
            method="post">
                <p name="nameT"> email </p>
                <input type="text" name="fname" placeholder="enter 
                 email" onblur="validateForm()">
                 <p name="passT"> password </p>
                 <input type="text" name="name" placeholder="enter 
                 password">
                 <br>
                 <input type="submit" value="Submit">
                 <br>
                 <a href="#"> 
                    Lost your password? 
                 </a>
                 <br>
                 <a href="#"> 
                    Create an account 
                 </a>
             </form>
           </div>
       </body>

     </html>

当我点击关闭电子邮件框时,警报消息会弹出,文本不会改变颜色。没有我知道的错误消息弹出。几乎就像跳过了更改颜色代码。

【问题讨论】:

    标签: javascript html forms validation styles


    【解决方案1】:

    你可以这样尝试:

          function validateForm() {
          
              var x = document.forms["myForm"]["fname"].value;
              if (x == "") {
              alert("something must be filled out");
              return false;
              }
              else if (!(x.includes("@"))) 
             {
               alert("you must have to filled value with @");
               document.getElementById("nameT").style.color="red";
               return false;
             }
            }
     html, body { height: 100%; }
       body {
         background:radial-gradient(ellipse at center, 
         rgba(255,254,234,1) 0%, rgba(255,254,234,1) 35%, #ffffff 100%);
         overflow: hidden;
         }
    
         .ocean { 
            height: 5%;
            width:100%;
            position:absolute;
            bottom:0;
            left:0;
            background: #ffffff;
          }
    
         .wave {
            background: url(https://s3-us-west- 
           2.amazonaws.com/s.cdpn.io/85486/wave.svg) repeat-x; 
           position: absolute;
           top: -198px;
           width: 6400px;
           height: 198px;
           animation: wave 7s cubic-bezier( 0.36, 0.45, 0.63, 0.53) 
           infinite;
           transform: translate3d(0, 0, 0);
          }
    
        .wave:nth-of-type(2) {
          top: -175px;
          animation: wave 7s cubic-bezier( 0.36, 0.45, 0.63, 0.53) 
          -.125s infinite, swell 7s ease -1.25s infinite;
          opacity: 1;
         }
    
        @keyframes wave {
         0% {
           margin-left: 0;
            }
         100% {
           margin-left: -1600px;
            }
           }
    
        @keyframes swell {
        0%, 100% {
           transform: translate3d(0,-25px,0);
         }
        50% {
         transform: translate3d(0,5px,0);
            }
           }
    
         .loginbox
        {
         width: 340px;
         height: 360px;
         background: #000;
         color: #fff;
         top: 50%;
         left:50%;
         position: absolute;
         transform: translate(-50%, -50%);
         box-sizing: border-box;
       }
      h2
       {
         margin: 0;
         padding: 0 0 20px;
         text-align: center;
         font-size: 22px; 
       }
    
    
    
      .loginbox input
      {
       width: 100%;
       margin-bottom: 20px;
      }
    
      .loginbox input[type="text"], input[type="password"]
      {
        border: none;
    
        border-bottom: 1px solid #fff;
        background: transparent;
        outline: none;
        height: 40px;
        color: #fff;
        font-size: 16px;
       }
    
       .loginbox input[type="submit"]
      {
        border: none;
        outline: none;
        height: 48px;
        background: #fb2525;
        color: #fff;
        font-size: 18px;
        border-radius: 20px;
       } 
    
       .loginbox input[type="submit"]:hover
       {
         cursor: pointer;
         background: #ffc107;
         color: #000;
       }
    
       .loginbox a
       {
        text-decoration: none;
        font-size: 12px;
        line-height: 20px;
        color: darkgrey;
       }
    
      .loginbox p
       {
         margin: 0;
         padding: 10px;
         font-weight: bold;
        }
        .loginbox a:hover
        {
          color: #ffc107;
         }
    <!DOCTYPE html>
     <html>
    
      <head>
        <title>
           Sign Up Page
        </title>
         <link rel="stylesheet" href="style.css" media="screen"/>
      </head>
    
      <body>
    
    
    
          <div class="ocean">
          <div class="wave"></div>
          <div class="wave"></div>
          </div>
          <h1>
             <center>
                Whale
             </center>
          </h1>
          <div class="loginbox">
    
            <h2>
              Login
            </h2>
            <form name="myForm" onsubmit="return validateForm()" 
            method="post">
                <p id="nameT"> email </p>
                <input type="text" name="fname" placeholder="enter 
                 email" onblur="validateForm()">
                 <p name="passT"> password </p>
                 <input type="text" name="name" placeholder="enter 
                 password">
                 <br>
                 <input type="submit" value="Submit">
                 <br>
                 <a href="#"> 
                    Lost your password? 
                 </a>
                 <br>
                 <a href="#"> 
                    Create an account 
                 </a>
             </form>
           </div>
       </body>
    
     </html>

    【讨论】:

    • 提示你所做的会像“china veerreddy”一样好
    • 你面临什么问题?
    • 与我在您的代码之前使用的代码相比没有任何变化。
    • 如果 email-id 确实有@,那么它会将 p 标签变为红色。
    • 运行时会发生什么?
    【解决方案2】:

    您必须将电子邮件的名称更改为 id 并添加一个错误类,请检查以下代码

     <script>
        function validateForm() {
          var x = document.forms["myForm"]["fname"].value;
          if (x == "") {
          alert("something must be filled out");
          return false;
          }
          else if (!(x.includes("@"))) 
         {
           alert("");
           document.getElementById("nameT").className="error";
           return false;
         }
        }
     </script>
    
      <div class="ocean">
      <div class="wave"></div>
      <div class="wave"></div>
      </div>
      <h1>
         <center>
            Whale
         </center>
      </h1>
      <div class="loginbox">
    
        <h2>
          Login
        </h2>
        <form name="myForm" onsubmit="return validateForm()" 
        method="post">
            <p id="nameT"> email </p>
            <input type="text" name="fname" placeholder="enter 
             email" onblur="validateForm()">
             <p name="passT"> password </p>
             <input type="text" name="name" placeholder="enter 
             password">
             <br>
             <input type="submit" value="Submit">
             <br>
             <a href="#"> 
                Lost your password? 
             </a>
             <br>
             <a href="#"> 
                Create an account 
             </a>
         </form>
       </div>
    

    【讨论】:

    • @Wargame Elliot 作为document.getElementById 的函数名称表示您必须使用id 属性来识别它。 name 属性在这种情况下是无用的。您可以将name 用于表单元素,然后您可以执行document.forms[$yourForm][$yourElementName] 之类的操作
    • 我试过了,代码还是不能正常运行。
    • 我忘记将 name 替换为 id ,现在已编辑,请立即尝试并确保为错误类添加样式,例如 .error{ color:red;}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2017-07-06
    • 2012-01-27
    • 2020-04-13
    • 2019-05-06
    • 2020-02-08
    • 1970-01-01
    相关资源
    最近更新 更多