Using a JavaScript class in ASP and passing "object.ClientId"

Posted on 2009-02-24
Last Modified: 2012-05-06
I have a JavaScript class called ControlValidation.js.  

On my ASP page I have a reference to that class using:
<script type="text/javascript" language="javascript" src="<%= Request.ApplicationPath %>/JS/ControlValidation.js"></script>

In my code behind I call the isValidPhoneNumber function using:
btnSave.Attributes.Add("OnClick", "return isValidPhoneNumber(" & tbPhoneNum.Text & ")")

JavaScript code looks like:
function isValidPhoneNumber(validationText)
    rePhoneNumber = new RegExp(/^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/);
    if (!rePhoneNumber.test(validationText))
        return false;

    return true;

While this works fine, I want to pass in the ClientId of an ASP label so I can set the text when the validation fails.  I tried btnSave.Attributes.Add("OnClick", "return isValidPhoneNumber(" & tbPhoneNum.Text & "," & lblErrorMessage.ClientId & ")") and in the JS function I added myMsg.value = "Invalid Phone Number" but this doesn't seem to work.

How can I pass the label to my JS function so I can assign text to it?
Question by:nightshadz
    LVL 16

    Accepted Solution

    try this as the onclick. in csharp it ClientID, not sure if it is different in vb
    ("OnClick", "return isValidPhoneNumber('" & tbPhoneNum.Text & "','" & lblErrorMessage.ClientId & "')")

    then just get the element by the clientID
    function isValidPhoneNumber(validationText,labelid)
    var label=document.getElementById(labelid);

    Author Closing Comment

    Awesome thanks!

    I also needed to add label.innerHTML = "my text"; to assign text to the label.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now