ASP.NET label Control and Javascript

Posted on 2004-11-03
Last Modified: 2006-11-17

I have User ID and  Password Text boxex.
 and I need to check if password contains user id(i.e password and user id should not be the same) and display errormessage using a ASP.NET label control

I'm doing this validation

function CheckPassword()
                  lblErrorMessage.Text=">>> Your password should not contain your UserID. Please enter a new password."
                  return false;
            return true;


I'm calling this function in form onsubmit event.
I'm getting the following message( lblErrorMessage is null or not defined').
lblErrorMessage is a label control with stylesheet class attached to it for color

What is wrong with this?
Question by:AnithaB
    LVL 1

    Expert Comment


    I don't think you can refer to your ASP.NET label control using Javascript. You will need to use VBScript or something along those lines.
    LVL 21

    Expert Comment

    ASP .Net Labels get rendered as <Span> . So after the page is generated check what gets generated. For Span you can try giving

    document.all.lblErrorMessage.innerHTML = "The username and passwords are same"

    Well there may be other possibilities too. Please let me know if the above solution works

    LVL 2

    Expert Comment

    You have to mention the document.forms(0).lblErrorMessage.value instead of

    note I had changed the text as value and document.forms(0) added in front

    try and give ur comments
    LVL 21

    Accepted Solution

    Hi Arun,

    Since the Label gets rendered as a span and span does not usually have a value attribute. So it is something like this

    <span>TEXT HERE</span>

    so "TEXT HERE" is the innerHTML part of it. So it has to be

    document.forms(0).lblErrorMessage.innerHTML = "Changed Text"


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now