如何使用 C#、VB.NET 在 ASP.NET 中启用 JAVASCRIPT

如何使用 C#、VB.NET 在 ASP.NET 中启用 JAVASCRIPT

简介:

在本例中,将创建一个注册网页。还将在每个输入框 (TextBox) 上应用 JavaScript 验证,以便在客户端验证数据。

在这里插入图片描述

但众所周知,JavaScript 可以从浏览器的设置中禁用,并且网页上应用的所有验证都将被绕过,即不会起作用。现在出现了如何强制启用 JavaScript 的问题,即它应该始终在客户端浏览器上运行。答案是否定的,我们不能。因为这是浏览器的安全问题,不允许我们通过代码启用或禁用它。唯一的方法是从浏览器的设置中手动更改它。所以我们只有一个选项来检查 JavaScript 在客户端浏览器上是启用还是禁用。如果禁用,则显示一条消息以从浏览器的设置中启用 JavaScript。有一个标记 < NOSCRIPT > 仅在浏览器禁用 JavaScript 时执行。因此,我们可以使用此标签向访问者显示带有说明链接的适当消息,以在他们的浏览器上启用 JavaScript。 实施: 让我们在 asp.net 中创建一个应用程序来理解这个概念。

在设计页面 (.aspx) 的 标签中创建如上图第一个所示的表单:

源代码:

<fieldset  style="width:280px;">
            <legend>Enable javaScript example in asp.net</legend>
            <table>
            <tr>
                <td>
                    Username <span style="color: #FF3300">*</span></td>
                <td>
                    <asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td >
                    Password <span style="color: #FF3300">*</span></td>
                <td>
                    <asp:TextBox ID="txtpwd" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td >
                    confirm password <span style="color: #FF3300">*</span></td>
                <td>
                    <asp:TextBox ID="txtcnmpwd" runat="server"></asp:TextBox>
                </td>
            </tr>

            <tr>
                <td >
                    First name <span style="color: #FF3300">*</span></td>
                <td>
                    <asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td >
                    Last name <span style="color: #FF3300">*</span></td>
                <td>
                    <asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td >
                    Email Address <span style="color: #FF3300">*</span></td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Phone number <span style="color: #FF3300">*</span></td>
                <td class="auto-style2">
                    <asp:TextBox ID="txtphone" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td >
                    Location <span style="color: #FF3300">*</span></td>
                <td>
                    <asp:TextBox ID="txtlocation" runat="server" Height="22px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td >
                    &nbsp;</td>
                <td>
                    <asp:Button ID="btnsubmit" runat="server" Text="Submit" />
                </td>
            </tr>
            
        </table>
        </fieldset>

在 标记中创建一个 JavaScript 函数来验证表单上的每个控件:

<script language="javascript" type="text/javascript">
          function validationCheck() {
              var summary = "";
              summary += isvaliduser();
              summary += isvalidpassword();
              summary += isvalidConfirmpassword();
              summary += isvalidFirstname();
              summary += isvalidLastname();
              summary += isvalidEmail();
              summary += isvalidphoneno();
              summary += isvalidLocation();

              if (summary != "") {
                  alert(summary);
                  return false;
              }
              else {
                  return true;
              }
          }
          function isvaliduser() {
              var id;
              var temp = document.getElementById("<%=txtuser.ClientID %>");
            id = temp.value;
            if (id == "") {
                return ("Please Enter User Name" + "\n");
            }
            else {
                return "";
            }
        }
        function isvalidpassword() {
            var id;
            var temp = document.getElementById("<%=txtpwd.ClientID %>");
            id = temp.value;
            if (id == "") {
                return ("Please enter password" + "\n");
            }
            else {
                return "";
            }
        }
        function isvalidConfirmpassword() {
            var uidpwd;
            var uidcnmpwd;
            var tempcnmpwd = document.getElementById("<%=txtcnmpwd.ClientID %>");
            uidcnmpwd = tempcnmpwd.value;
            var temppwd = document.getElementById("<%=txtpwd.ClientID %>");
            uidpwd = temppwd.value;

            if (uidcnmpwd == "" || uidcnmpwd != uidpwd) {
                return ("Please check and re-enter password to confrim" + "\n");
            }
            else {
                return "";
            }
        }
        function isvalidFirstname() {
            var id;
            var temp = document.getElementById("<%=txtfname.ClientID %>");
            id = temp.value;
            if (id == "") {
                return ("Please enter first name" + "\n");
            }
            else {
                return "";
            }
        }
        function isvalidLastname() {
            var id;
            var temp = document.getElementById("<%=txtlname.ClientID %>");
            id = temp.value;
            if (id == "") {
                return ("Please enter last name" + "\n");
            }
            else {
                return "";
            }
        }
        function isvalidEmail() {
            var id;
            var temp = document.getElementById("<%=txtEmail.ClientID %>");
            id = temp.value;
            var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
            if (id == "") {
                return ("Please Enter Email" + "\n");
            }
            else if (re.test(id)) {
                return "";
            }

            else {
                return ("Email should be in the form abc@xyz.com" + "\n");
            }
        }
        function isvalidphoneno() {
            var id;
            var temp = document.getElementById("<%=txtphone.ClientID %>");
            id = temp.value;
            var re;
            re = /^[0-9]+$/;
            var digits = /\d(10)/;
            if (id == "") {
                return ("Please enter phone no" + "\n");
            }
            else if (re.test(id)) {
                return "";
            }

            else {
                return ("Phone no should be digits only" + "\n");
            }
        }
        function isvalidLocation() {
            var id;
            var temp = document.getElementById("<%=txtlocation.ClientID %>");
            id = temp.value;
            if (id == "") {
                return ("Please enter Location" + "\n");
            }
            else {
                return "";
            }
        }
</script>


现在运行应用程序。在不输入任何内容或输入错误数据的情况下单击提交按钮,它将显示验证失败消息,如下图所示:
在这里插入图片描述
但是,如果客户端即用户/访问者已从其浏览器设置中禁用了 JavaScript,则在单击“提交”按钮时将不会显示任何验证失败消息。所以我们必须使用标签。

因此,将以下标记也放在 标记中

<noscript>  <h3 style="color: #FF3300;">
Javascript is not currently enabled in your browser.<br /> You must <a href="http://support.google.com/bin/answer.py?hl=en&answer=23852" target="_blank" >enable Javascript </a> to run this web page correctly. 
</h3></noscript>


现在是调用为检查验证而创建的 JavaScript 函数的时候了。

C#.Net 代码 强制启用 javascript

因此在代码隐藏文件 (.aspx.cs) 中调用页面加载事件上的 JavaScript 函数:

protected void Page_Load(object sender, EventArgs e)
    {
        btnsubmit.Attributes.Add("onclick", "javascript:return validationCheck()");
    }

强制启用 javascript 的VB.Net 代码

在代码隐藏文件 (.aspx.vb) 中调用页面加载事件上的 JavaScript 函数:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        btnsubmit.Attributes.Add("onclick", "javascript:return validationCheck()")
    End Sub

现在从浏览器设置中禁用 JavaScript 并再次运行该应用程序。它将向您显示启用 JavaScript 的消息,如下图所示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值