ABC
ABC

Reputation: 181

Error while calling a javascript function in IE11

below is the js function that I have created on the page:

<script type="text/javascript">
    function maxLength(field, maxChars) {
        if (field.value.length >= maxChars) {
            event.returnValue = false;
            return false;
        }
    }

</script>

and this is the textarea for which I am calling this function :

<textarea id="txtapproverremarks" cols="20" rows="2" runat="server" style="width: 600px"
                                                            onkeypress="javascript:return maxLength(this,'50');"></textarea>

The things are working fine on IE7/8/9 but giving below error in IE 10 & IE 11 :

JavaScript runtime error: Function expected

Anyone have any idea, why this is happening.

Thanks in advance.

Upvotes: 1

Views: 1469

Answers (2)

P&#233;ter
P&#233;ter

Reputation: 2181

You should use some framework or library to validate fields. For example jquery with validate plugin: http://jqueryvalidation.org/
If you use ASP.NET WebForms than you can also use the validation controls: http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx

EDIT:
If you have to change this code because of the IE11 malfunction, you also have to change it on every page you used. I think your approach is wrong. It cause small maintainability and flexibility. It's good practise to use what is used by most of the developer. So if another developer join to the project the warm up time is reduced and the common library has better support than custom solution.

The error itself suggest that the problem is not inside of your function. The parser isn't recognize the maxLentgh expression as a valid function in the context. Change the name of the function for example maxLength2.
I think you run into some internal variable that has precedence over the global function. (This kind of problems won't emerge if you use some commmon solution)

Upvotes: 0

Bhavesh Kachhadiya
Bhavesh Kachhadiya

Reputation: 3972

Comment the event.returnValue statement.I think it is not needed and the rest of code should work perfectly.

<script type="text/javascript">
    function maxLength(field, maxChars) {
        //if (field.value.length >= maxChars) {
            //event.returnValue = false;
        //    return false;
        //}
        //Just return required result
        return (field.value.length <= maxChars);
    }

</script>

Also you need to change the your HTML, particularly your javascript function call.

<textarea id="txtapproverremarks" cols="20" rows="2" 
runat="server" style="width: 600px"
 onkeypress="javascript:return maxLength(this,50);"></textarea>

Upvotes: 1

Related Questions