I have an input field with the id mobileNumber
, it only returns numbers as input So, I have a function (Event Handler Function) written that checks the input. But if you add functions through the addEventListener
function, this will not work, but if you add via the onkeypress
attribute, then it works.
For example
// Input field; Var isNumeric = function (event) {var is specialKeys = new Array (); SpecialKeys.push (8); // Backspace var keyCode = event.which? Event.which: event.keyCode var ret = ((keycode> = 48 & amp; amp; amp; amp; amp; codes & lt; = 57)) specialKeys.indexOf (KeyCode)! = -1); Return writ; }; // This function checks whether the input character is digit or not eventObj ["numeric"] = function (event) {var specialKeys = new Array (); SpecialKeys.push (8); // Backspace var keyCode = event.which? Event.which: event.keyCode var ret = ((keycode> = 48 & amp; amp; amp; amp; amp; codes & lt; = 57)) specialKeys.indexOf (KeyCode)! = -1); Return writ; }; // In order to work in event oz, the same functionality (function (event_object) {var mobileInput = document.getElementById ("mobile number"), mobileInput.adventelisoner ("keypress", event oz ["numeric"], false) ;}) (EventObj);
If I connect an event listener in such a way, the function is called but the user does not restrict characters from logging in;
But if I do this
& lt; Input type = "text" class = "insidetxt" placeholder = "mobile number" name = "mobile number" id = "mobileNumber" onkeypress = "return numerical (event);" & Gt;
this works fine
Am I doing something wrong in another way?
Yes, the second approach is not the best practice HTML attribute can be changed or bypassed, Therefore it is better to add event listeners.
No comments:
Post a Comment