Javascript验证无法在C#中工作(错误消息)

时间:2016-08-19 10:06:46

标签: javascript c#

以下是javascript验证码:

namespace UnityEngine
{
 class Foo
 {
  void SpawnEntity()
  {
   //The actual code that does something in the game
  }
 }
}

这是文本框:

if (document.getElementById("<%=txtCategoryName.ClientID %>").value.trim() == "") {
document.getElementById("<%=dvMessage.ClientID %>").innerHTML = "Please enter Category.";

document.getElementById("<%=txtCategoryName.ClientID %>").focus();

document.getElementById("<%=txtCategoryName.ClientID %>").classList.add("ErrorControl");
// document.getElementById("<%=txtCategoryName.ClientID %>").style.outline = '1px solid red';
// document.getElmentById("<%=txtCategoryName.ClientID %>").style.border = '3px solid red';
return false;

}
if (document.getElementById("<%=txtCategoryDescription.ClientID %>").value.trim() == "") {
document.getElementById("<%=dvMsg.ClientID %>").innerHTML = "Please enter Category Description.";
document.getElementById("<%=txtCategoryDescription.ClientID %>").focus();
document.getElementById("<%=txtCategoryDescription.ClientID %>").classList.add("ErrorControl");
// document.getElementById("<%=txtCategoryDescription.ClientID %>").style.outline = '1px solid red';
return false;
}
}

<script>
function checkfunction(val) {
if (val != "") {

document.getElementById("<%=txtCategoryName.ClientID %>").classList.remove("ErrorControl");
// document.getElementById("<%=dvMessage.ClientID %>").style.display = 'none';
document.getElementById("<%=txtCategoryDescription.ClientID %>").classList.remove("ErrorControl");
// document.getElementById("<%=dvMsg.ClientID %>").style.display = 'none';
}
}
</script>

问题是验证无法正常工作。错误消息显示我遇到了问题。请告诉我如何处理多个文本框和下拉列表以及如何使用javascript验证?

1 个答案:

答案 0 :(得分:0)

先生,你需要这样的东西吗?正如Abhay所说,你可以使用大量的插件,jquery只是一个javascript库。

<script>
var catName = {
        'label' : document.getElementById("<%=dvMessage.ClientID %>"),
        'textbox' : document.getElementById("<%=txtCategoryName.ClientID %>")
    },
    catDesc = {
        'label' : document.getElementById("<%=dvMsg.ClientID %>"),
        'textbox' : document.getElementById("<%=txtCategoryDescription.ClientID %>")    
    },
    noText = function hasText( textbox ) {
        return textbox.value.trim().length === 0;
    };
// This entire part might be redundant, unless the textbox comes back from the server with text already inside it.
// If the textbox is always empty at startup, this part could be replaced by placing the text already into the div.
if (noText(catName.textbox)) {
    catName.label.innerHTML = "Please enter Category.";
    catName.textbox.focus()
    catName.textbox.classList.add("ErrorControl");
}
if (noText(catDesc.textbox)) {
    catDesc.label.innerHTML = "Please enter Category Description.";
    catDesc.textbox.focus()
    catDesc.textbox.classList.add("ErrorControl");
}

function checkfunction( textValue ) {
    var catNameCls = catName.textbox.classList,
        catDescCls = catDesc.textbox.classList;
    if (textValue.length !== 0) {
        if (catNameCls.contains("ErrorControl")) catNameCls.remove("ErrorControl");
        else catNameCls.add("ErrorControl");
        if (catDescCls.contains("ErrorControl")) catDescCls.remove("ErrorControl");
        else catDescCls.add("ErrorControl");        
    }
}
</script>