从ASP.NET文本框使用JavaScript [英] Using javascript from an ASP.NET textbox

查看:104
本文介绍了从ASP.NET文本框使用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前做一个ASP.Net页面作为项目的一部分;
同时使主注册/登录页面验证程序(的标签,改变他们recpective文本框的OnChange事件及其对javascript和触发知名度),我面临的一个问题。 Allthough它在我们的计算机实验室工作完全正常(这意味着JavaScript的code本身可能是正确的),校验器并不在所有的工作 - 不管输入的。
任何想法,为什么会发生呢?

感谢您!

使用Javascript:

 函数isUserValid(){
    VAR UserLength =的document.getElementById(UserTB)value.length。
    变种ValidatorLabel =的document.getElementById(的ValidateUser);
    如果(UserLength 6; || UserLength→15){
        ValidatorLabel.style.display =内联;
        返回false;
        }
    其他{
        ValidatorLabel.style.display =无;
        返回true;
    }
    }
功能isPassValid(){
        VAR PASSLENGTH =的document.getElementById(PasswordTB)value.length。
        变种ValidatorLabel =的document.getElementById(ValidatePassword);
        如果(PASSLENGTH 6; || PASSLENGTH→15){
            ValidatorLabel.style.display =内联;
            返回false;
        }
        其他{
            ValidatorLabel.style.display =无;
            返回true;
        }
    }
功能isConfirmValid(){
        VAR密码=的document.getElementById(PasswordTB)值。
        VAR我=的document.getElementById(ConfirmTB)值。
        变种ValidatorLabel =的document.getElementById(ValidateConfirm);
        如果(密码==我){
            ValidatorLabel.style.display =无;
            返回true;
        }
        其他{
            ValidatorLabel.style.display =内联;
            返回false;
        }
    }
功能isEmailValid(){
        无功海峡=的document.getElementById(EmailTB)值。
        VAR lastAtPos = str.lastIndexOf('@');
        变种lastDotPos = str.lastIndexOf('。');
        变种isFine =(lastAtPos&所述; lastDotPos&放大器;&放大器; lastAtPos大于0&放大器;&放大器; str.indexOf('@@')== -1&放大器;&放大器; lastDotPos→2&放大器;及(str.length - lastDotPos)→2);
        变种ValidationLabel =的document.getElementById(ValidateEmail);
        如果(isFine)
        {
            ValidationLabel.style.display =无;
            返回true;
        }
        其他
        {
            ValidationLabel.style.display =内联;
            返回false;
        }
    }

在ASP:

 <脚本SRC =Validators.js类型=文/ JavaScript的>< / SCRIPT>
....

ASP验证:

 用户名:其中; BR />
    < ASP:文本框ID =UserTB=服务器的OnChange =返回isUserValid();的AutoPostBack =false的>< / ASP:文本框>
    < ASP:标签ID =的ValidateUser=服务器前景色=红
        文本=用户名长度必须在6-15个字符,并且不包含特殊字符。的CssClass =校验>< / ASP:标签>
    < BR />< BR />
    密码:LT; BR />
    < ASP:文本框ID =PasswordTB=服务器的OnChange =返回isPassValid();的AutoPostBack =false的>< / ASP:文本框>
    < ASP:标签ID =ValidatePassword=服务器前景色=红
        文本=密码长度必须在6-15个字符,并且不包含特殊字符。的CssClass =校验>< / ASP:标签>
    < BR />< BR />
    确认密码:LT; BR />
    < ASP:文本框ID =ConfirmTB=服务器的OnChange =返回isConfirmValid();的AutoPostBack =false的>< / ASP:文本框>
    < ASP:标签ID =ValidateConfirm=服务器前景色=红
        文本=此字段必须与密码字段相匹配。的CssClass =校验>< / ASP:标签>
    < BR />< BR />
    电子邮件:< BR />
    < ASP:文本框ID =EmailTB=服务器的OnChange =返回isEmailValid();的AutoPostBack =false的>< / ASP:文本框>
    < ASP:标签ID =ValidateEmail=服务器前景色=红楼梦文本=无效的电子邮件。的CssClass =校验>< / ASP:标签>


解决方案

 <结构>
    <&的System.Web GT;
        <页面的ClientIDMode =静态/>
    < /system.web>
< /结构>

在web.config中级别更改客户端ID模式,使您的ID在客户端和服务器端的将是相同的。或者,在你的标记设置在页面或控件级通过其各自的属性。

I am currently making an ASP.Net page as a part of a project; While making the main registration/Login page validators (Labels that change their visibility on javascript and trigger on the OnChange event of their recpective textboxes), I faced a problem. Allthough it worked perfectly fine in our computer labs (which means the javascript code itself is probably correct), the validators do not work at all - regardless of the input. Any idea why would it possibly happen?

Thank you!

Javascript:

function isUserValid() {
    var UserLength = document.getElementById("UserTB").value.length;
    var ValidatorLabel = document.getElementById("ValidateUser");
    if (UserLength < 6 || UserLength > 15) {
        ValidatorLabel.style.display = 'inline';
        return false;
        }
    else {
        ValidatorLabel.style.display = 'none';
        return true;
    }  
    }
function isPassValid() {
        var PassLength = document.getElementById("PasswordTB").value.length;
        var ValidatorLabel = document.getElementById("ValidatePassword");
        if (PassLength < 6 || PassLength > 15) {
            ValidatorLabel.style.display = 'inline';
            return false;
        }
        else {
            ValidatorLabel.style.display = 'none';
            return true;
        }
    }
function isConfirmValid() {
        var Password = document.getElementById("PasswordTB").value;
        var Me = document.getElementById("ConfirmTB").value;
        var ValidatorLabel = document.getElementById("ValidateConfirm");
        if (Password == Me) {
            ValidatorLabel.style.display = 'none';
            return true;
        }
        else {
            ValidatorLabel.style.display = 'inline';
            return false;
        }
    }
function isEmailValid() {
        var str = document.getElementById("EmailTB").value;
        var lastAtPos = str.lastIndexOf('@');
        var lastDotPos = str.lastIndexOf('.');
        var isFine = (lastAtPos < lastDotPos && lastAtPos > 0 && str.indexOf('@@') == -1 && lastDotPos > 2 && (str.length - lastDotPos) > 2);
        var ValidationLabel=document.getElementById("ValidateEmail");
        if(isFine)
        {
            ValidationLabel.style.display='none';
            return true;
        }
        else
        {
            ValidationLabel.style.display='inline';
            return false;
        }
    }

In the ASP:

<script src="Validators.js" type="text/javascript"></script>
....

ASP "validators":

Username:<br />
    <asp:TextBox ID="UserTB" runat="server" OnChange="return isUserValid();" AutoPostBack="false"></asp:TextBox>
    <asp:Label ID="ValidateUser" runat="server" ForeColor="Red"
        Text="Username must be 6-15 characters in length, and contain no special characters." CssClass="Validators"></asp:Label>
    <br /><br />
    Password:<br />
    <asp:TextBox ID="PasswordTB" runat="server" OnChange="return isPassValid();" AutoPostBack="false"></asp:TextBox>
    <asp:Label ID="ValidatePassword" runat="server" ForeColor="Red"
        Text="Password must be 6-15 characters in length, and contain no special characters." CssClass="Validators"></asp:Label>
    <br /><br />
    Confirm password:<br />
    <asp:TextBox ID="ConfirmTB" runat="server" OnChange="return isConfirmValid();" AutoPostBack="false"></asp:TextBox>
    <asp:Label ID="ValidateConfirm" runat="server" ForeColor="Red"
        Text="This field must match the password field." CssClass="Validators"></asp:Label>
    <br /><br />
    Email:<br />
    <asp:TextBox ID="EmailTB" runat="server" OnChange="return isEmailValid();" AutoPostBack="false"></asp:TextBox>
    <asp:Label ID="ValidateEmail" runat="server" ForeColor="Red" Text="Invalid Email." CssClass="Validators"></asp:Label>

解决方案

<configuration>    
    <system.web>
        <pages clientIDMode="Static" />
    </system.web>
</configuration>

Change the Client ID Mode at the web.config level so that your ID's on the client side and the server side will be the same. Alternatively, set it at the Page or Control level through their respective attributes in your markup.

这篇关于从ASP.NET文本框使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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