如何改变未经修复的领域的颜色? [英] How to change the color of unreqiured field?

查看:63
本文介绍了如何改变未经修复的领域的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果留空,我想更改输入的颜色。



我验证它是否输入了必须输入的内容



HTML code:

I want to change the color of input if left blank.

I validate it to see if user enter what must be entered

HTML code:

<div class="span4">
<label for="companyname">Company Name:</label>				
<input type="text" id="companyname" name="companyname" type="hidden" autofocus />
<span class="form_hint">Company name number must be: E.g "iRecall Recordings"</span>				       
</div>

< br $> b $ b



<pre lang="cs">onkeyup="checkCompanyname(this.value);"
                onblur="checkCompanyname(this.value);" value=""/>







我的验证码:




My validation code:

var status = document.getElementById("companyname");
var companyname = document.getElementById("companyname");

function checkCompanyname(companyname)
{

    var regCompanyname = /^[.@&]?[a-zA-Z0-9 ]+[ !.@&()]?[ a-zA-Z0-9!()]+/;

    if(regCompanyname.test(companyname) == false) {
    document.getElementById("companyname").style.backgroundColor="#FBBBB9";
    } else if(regCompanyname.test(companyname) == true) {
        document.getElementById("companyname").style.backgroundColor="#FFFFFF";
    }
}





如果用户未在输入中输入任何内容或将其空白,则应保留白色。如果用户输入内容,则应将颜色更改为红色,如果输入正确,则必须为白色。这必须只发生在不需要的字段上。



那我怎么能做到这一点?



If user does not enter anything on the input or live it blank, it should stay white in color. If user enter something it should change color to red, if input is correct, it must be white. This must happen only to fields that are not required.

So how can I accomplish this?

推荐答案

如果字段包含不正确的数据,您希望颜色为红色吗?在所有其他情况下,它应该是白色的吗?



如果是这样,为什么不删除否则如果并将其更改为否则

Do you want the color to be red if the the field contains incorrect data and in all other cases it should be white?

If so, why not remove the else if and change it to else?
if(regCompanyname.test(companyname) == false) {
    document.getElementById("companyname").style.backgroundColor="#FBBBB9";
    } else {
        document.getElementById("companyname").style.backgroundColor="#FFFFFF";
}





[更新]

请尝试使用此方法或其变体。



[UPDATE]
Try this approach instead or a variant there of.

if (companyname == '') {
    document.getElementById("companyname").style.backgroundColor="#FFFFFF";
} else if(regCompanyname.test(companyname) == false) {
    document.getElementById("companyname").style.backgroundColor="#FBBBB9";
} else {
    document.getElementById("companyname").style.backgroundColor="#FFFFFF";
}


这篇关于如何改变未经修复的领域的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆