简体   繁体   English

如何使用JavaScript显示asp.net页的验证摘要

[英]how to display validation summary of an asp.net page using Javascript

I have one asp.net form. 我有一个asp.net表格。
In it i have many fields which are required. 在其中,我有许多必填字段。
I want to display validation summary of all fields at the end of the form. 我想在表单末尾显示所有字段的验证摘要。
I have already checked for valid values of input controls. 我已经检查了输入控件的有效值。
Now i only want is Summary. 现在我只想要摘要。

Here is my code for valid data input 这是我输入有效数据的代​​码

<script language="javascript" type="text/javascript">
        function userCheck(uName) {
            var uName = document.getElementById(uName);
            var letters = /^[A-Za-z0-9]+$/;
            if (uName.value.match(letters) && uName.value.length != 0) {
                uName.style.border = "2px solid #008A2E";
                return true;
            }
            else {
                uName.value = uName.value.replace(/[\W]/g, '');
                uName.style.border = "2px solid #ff0000";
                uName.focus();
                return false;
            }
        }
</script>

This is just one function for username check. 这只是用户名检查的一项功能。
I have many more to deal with. 我还有很多要处理的。
Is there any way to display summary from all fields at the last when submit button is pressed ? 按提交按钮时,有什么方法可以显示所有字段的摘要?
below solution is not working. 下面的解决方案不起作用。

 function ddlcheck(ddlclg) {
            var clg = document.getElementById(ddlclg.id);
            var clgname = document.getElementById('<%= LblCollegeName.ClientID %>');
            clgname.style.display = "block";
            clgname.innerHTML = "Selected College : " + clg.options[clg.selectedIndex].value;
            clg.style.border = "1px solid #008A2E";
            if (clg.options[clg.selectedIndex].value == "Select") {
                clgname.style.display = "none";
                clg.style.border = "1px solid #ff0000";
                validationhtml = validationhtml + "<b>*</b> College" + "</br>";
            }
        }

above code is for dropdownlist. 上面的代码用于下拉列表。

 function select() {
            var count = 0;
            var chkSelectAll = document.getElementById('<%= ChkSelectAll.ClientID %>');
            var chkList = document.getElementById('<%= chk.ClientID %>').getElementsByTagName("input");
            for (var i = 0; i < chkList.length; i++) {
                if (chkList[i].checked == true) {
                    count++;
                }
            }
            if (count == chkList.length)
                chkSelectAll.checked = true;
            else {
                chkSelectAll.checked = false;
            }
        }

above code is for selected check boxes. 上面的代码适用于选定的复选框。

create a div ( errorreport) at required location validation summary give it style as you needed 在所需的位置验证摘要中创建div(错误报告),根据需要提供样式

After that 之后

<script language="javascript" type="text/javascript">
    var validationhtml="";
    function userCheck(uName) {
        var uName = document.getElementById(uName);
        var letters = /^[A-Za-z0-9]+$/;
        if (uName.value.match(letters) && uName.value.length != 0) {
            uName.style.border = "2px solid #008A2E";
            return true;
        } else {
            uName.value = uName.value.replace(/[\W]/g, '');
            uName.style.border = "2px solid #ff0000";
            uName.focus();
            validationhtml=validationhtml +"uname is not correct" ;
            return false;
        }
    }

    function validationsummary() {

        // if using jquery
        $(".errorreport").html(validationhtml); 

        //for javascript
        document.getelementbyid("errorreport").innerHTML = validationhtml; 

        if(validationhtml == "") {
            return true;
        } else {
            return false;
        }
    }
</script>

and call validationsummary() on submit button click 并在提交按钮单击时调用validationsummary()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM