Javascript验证不会覆盖原始css表单颜色

时间:2012-06-30 20:43:27

标签: javascript html forms validation

任何人都可以看到我可能犯错的地方吗?表单文本框背景颜色最初设置为灰色。如果用户犯了错误,我想把它们变成黄色并带有红色边框。该函数有点工作,因为当表单填写不正确时,表单不会进入服务器。但是css并没有改变。如果我注释掉我的js调用它将发布到服务器。以下是片段:

CSS:

  .invalid{
background-color:#ff9;
border: 2px red inset;
}

   .reqd{
background-color:#222222; 
color:#555555; border-style: solid; 
border-color:#555555; 
border-width: 1px;
 }

HTML:

<!DOCTYPE html>
  <?php 
  // Debug
   error_reporting(E_ALL);
  ?>

<html lang="en">
 <head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css" type="text/css">
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/regauth.js"></script>

    <title></title>
 </head>
 <body id="registerBody">
<section id="registerBox">

    <form id="registerForm" method="post" action="regauth.php">
    <p>Welcome to the atlas registraton! Please fill out the information below.</p>
    <br>
    <p><label for="firstName">First Name:&nbsp;&nbsp;<input type="text" size="30" id="firstName" name="firstName" class="reqd"></label></p>
    <p><label for="lastName">Last Name:&nbsp;&nbsp;<input type="text" size="30" id="lastName" name="lastName" class="reqd"></label></p>
    <p><label for="email">Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" size="30" id="email" name="email" class="reqd"></label></p>
    <br>
    <br>
    <p><label for="reqUsername">Username:&nbsp;&nbsp;<input type="text" size="30" id="reqUsername" name="reqUsername" class="reqd"></label></p>
    <p><label for="passOne">Password:&nbsp;&nbsp;<input type="password" size="30" id="passOne" name="passOne" class="reqd"></label></p>
    <p><label for="passTwo">Confirm Password:&nbsp;&nbsp;<input type="password" size="30" id="passTwo" name="passTwo" class="reqd"></label></p>
    <br>
    <br>
    <p><input type="submit" value="Submit">&nbsp;&nbsp;<input type="reset" value="Reset Form" class="reset"></p>

    </form> 
</section>
<script type="text/javascript">
$("#registerBox").css("margin-left", ($(window).width()-425)/2);
$("#registerBox").css("margin-top", ($(document).height()-500)/2);
$('#registerBox').fadeIn(1500);
</script>

   </body>

    </html>

JS(regauth.js): 由Tom Negrino提供,Visual Quickstart指南:Javascript第八版

window.onload = initForms;

//Function loops through each form. For each one it adds an event handler to that forms 'onSubmit'.
function initForms() {
for (var i = 0; i < document.forms.length; i++) {
    document.forms[i].onsubmit = validForm;
     }
   }

function validForm() {
var allGood = true;
var allTags = document.getElementsByTagName("*");

for (var i = 0; i < allTags.length; i++) {
    if (!validTag(allTags[i])) {
        allGood = false;
    }
}
return allGood;

function validTag(thisTag) {
    var outClass = "";
    var allClasses = thisTag.className.split(" ");

    for (var j = 0; j < allClasses.length; j++) {
        outClass += validBasedOnClass(allClasses[j]) + " ";
    }
    thisTag.className = outClass;

    if (outClass.indexOf("invalid") > -1) {
        thisTag.focus();
        if (thisTag.nodeName == "INPUT") {
            thisTag.select();
        }
        return false;
    }
    return true;

    function validBasedOnClass(thisClass) {
        var classBack = "";

        switch (thisClass) {
            case "":
            case "invalid":
                break;
            case "reqd":
                if (allGood && thisTag.value == "") {
                    classBack = "invalid ";
                }
                classBack += thisClass;
                break;
            default:
                classBack += thisClass;
        }
        return classBack;
    }
}

}

1 个答案:

答案 0 :(得分:0)

在“reqd”之后将“无效”CSS块移动到

CSS的规则是“最后一条规则获胜”(排序;它更复杂但在这种情况下是问题)。

即使“无效”规则适用于无效元素,因为“reqd”规则具有背景颜色,并且因为“无效”规则之后,所以该设置适用。