1

I have the code

<script type="text/javascript">
    function validateLength(objectSource, args) {
        var a = document.getElementById('<%=txtUserName.ClientID%>').value;
        args.IsValid = (a.length >= 5 && a.length <= 10);
    }
</script>

Which I have in my ASP.NET page.

This works fines. Will validate a textbox to make sure it is between a certain length.

However I am trying to tidy up my page's code. So decided to put this little function into a separate javascript file.

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

Page loads fine and then when the validation comes around it fails. I am guessing it cannot access the textbox( txtUserName).

My question is: How can I get this to work?

I get the error message

0x800a138f - Microsoft JScript runtime error: Unable to get value of the property 'value': object is null or undefined

when the javascript fires

10
  • 4
    <script type="text/javascript" src"Scripts/MyJS.js"> -> <script type="text/javascript" src="Scripts/MyJS.js"> Commented Sep 10, 2015 at 14:57
  • yeah you forgot an = by src Commented Sep 10, 2015 at 14:59
  • @messerbill that was me forgetting while typing the code here. In my actual code there is an = Commented Sep 10, 2015 at 15:00
  • document.getElementById('<%=txtUserName.ClientID%>') is not found / existent on your page. Have you spelled it correctly? Commented Sep 10, 2015 at 15:05
  • @messerbill the code works fine when the script is in the page itself. But when loading from a .js file it cannot find it Commented Sep 10, 2015 at 15:06

2 Answers 2

4

When you include a javascript file, the part of code

<%=txtUserName.ClientID%>

isn't rendered.

You have to change the javascript function in two ways Fixing the name of the control

<script type="text/javascript">
    function validateLength(objectSource, args) {
        var a = document.getElementById('txtUserName').value;
        args.IsValid = (a.length >= 5 && a.length <= 10);
    }
</script>

or adding a parameter

<script type="text/javascript">
    function validateLength(objectSource, args, nameOfControl) {
        var a = document.getElementById(nameOfControl).value;
        args.IsValid = (a.length >= 5 && a.length <= 10);
    }
</script>

then you can call the function from your page and the parameter nameOfControl could be

<%=txtUserName.ClientID%>

for example

validateLength(objectSource, args, <%=txtUserName.ClientID%>)
Sign up to request clarification or add additional context in comments.

1 Comment

Adding the element name worked great. Thank you. I knew it would be something simple.
2

The <%=txtUserName.ClientID%> runs in context of your page. That will not work in a separate .js file. Separate .js files won't run any asp.net code. You could do something like:

JS file:

function validateLength(objectSource, args) {
    var a = document.getElementById(window.myValidatorId).value;
    args.IsValid = (a.length >= 5 && a.length <= 10);
}

ASP

<script type="text/javascript">
    window.myValidatorId = '<%=txtUserName.ClientID%>';
</script>

Or perhaps read the id from a hidden input field on the page.

1 Comment

but saving data in the browser is not beautiful, i would send it as a param via a normal function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.