如何在验证时更改文本框的背景颜色

编程入门 行业动态 更新时间:2024-10-25 13:14:37
本文介绍了如何在验证时更改文本框的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

要精确, On输入不正确的值到文本框我需要获得红色背景并再次修正红色背景值应该消失。它应该是Javascript。 我可以将背景颜色更改为红色但是在修正红色值时仍然可以。 我需要在正确验证时清除红色。

解决方案

您好, 尝试此解决方案:

< input type =textid =subEmailonkeyup =checkFilled();/>

JavaScript:

function checkFilled(){ var inputVal = document.getElementById( subEmail)。值; if (inputVal == Red ){ document.getElementById( subEmail)。style.backgroundColor = 红色; } else if (inputVal == white){ document.getElementById( subEmail)。style.backgroundColor = white; } }

根据您的要求,您可以更改颜色和输入值验证。

HTML

< input type =textid =phonemaxlength =10>

JavaScript:

(document).ready (function(){

To be PRECISE, On Entering improper value to textbox i need to get red background & again on correcting the value red background should disappear. It should be in Javascript. I am able to change background color to red but on correcting the value the red remain. I need to clear red color on proper validation.

解决方案

Hi, Try this solution :

<input type="text" id="subEmail" onkeyup="checkFilled();"/>

JavaScript :

function checkFilled() { var inputVal = document.getElementById("subEmail").value; if (inputVal == "Red") { document.getElementById("subEmail").style.backgroundColor = "Red"; } else if(inputVal == "white"){ document.getElementById("subEmail").style.backgroundColor = "white"; } }

As per your requirnment you can change the colors and input value validation.

HTML

<input type="text" id="phone" maxlength="10">

JavaScript :

(document).ready(function() {

更多推荐

如何在验证时更改文本框的背景颜色

本文发布于:2023-06-09 04:10:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/594964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   颜色   背景   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!