• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

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

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?
1 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);
nightshadzAuthor Commented:
Awesome thanks!

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now