Search code examples
javascriptc#onkeyup

How to add javascript in page_load


I have javascript code that mask my data in textbox by onkeyup event .

<script type="text/javascript">
document.getElementById("<%= yourTextbox.ClientID %>").onkeyup = function() {
    if (/^[0-9]{2}(.[0-9]{2})?$/.test(this.value)) {
        this.value += ".";
    }
}
<script> 

But I have error

Runtime Error JavaScript: Failed to set property "onkeyup" reference value is not defined or is NULL.

What i need to add in my code to make this work? Asp.net c# framework 2.0 javascript


Solution

  • in C# code on PageLoad

    yourTextbox.Attributes.Add("onkeyup","myFun('"+yourTextbox.ClientID+"');");
    

    in Javascript

    <script type="text/javascript">
     function myFun(obj) {
        if (/^[0-9]{2}(.[0-9]{2})?$/.test(obj.value)) {
            obj.value += ".";
        }
    }
    <script>