简体   繁体   中英

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

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> 

You must try to add the event with addEventListener.

document.getElementById("myBtn").addEventListener("onkeyup", function(){
        if (/^[0-9]{2}(.[0-9]{2})?$/.test(this.value)) {
            this.value += ".";
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM