Skip to content Skip to sidebar Skip to footer

Why My Code Doesn't Allow Specific Symbols?

I am using this code to allow only digits to type in textbox but now I want to allow . too. I modified this code but not working. function isNumberKeyDotAllowed(evt) { var char

Solution 1:

I see two problems with your code. The first is that you pass this as the argument to isNumberKeyDotAllowed while you should pass event:

onkeypress="return isNumberKeyDotAllowed(event);"

The second is the validation condition. Here is my own version of the function. I defined the condition for success instead of the condition for failure, because it is easier for me to figure out:

functionisNumberKeyDotAllowed(evt) {
    var charCode = evt.which ? evt.which : evt.keyCode;
    if (charCode == 46 || (48 <= charCode && charCode <= 57)) {
        returntrue;
    }
    else {
        returnfalse;
    }
}

Post a Comment for "Why My Code Doesn't Allow Specific Symbols?"