Use 'getElementsByname' but it don't helps me. What I'm doing wrong? "isAllowedSymbol" function is working great but not "checkNumbers".
function checkNumbers(this) {
var element = document.getElementsByname('answer')[0];
if (element != null && element.value.length == 10) {
element = element.replace(element, '');
}
}
function isAllowedSymbol(input) {
var value = input.value;
var rep = /[a-zA-Z]/;
var rep2 = /[а-яА-Я]/;
if (rep.test(value)) {
value = value.replace(rep, '');
input.value = value;
if (rep2.test(value)) {
value = value.replace(rep2, '');
input.value = value;
}
}
}
<input type="text" maxlength="10" onkeyup="isAllowedSymbol(this);checkNumbers(this); " placeholder="Enter data" name="answer" "> <br>
You can not accept
"this"
as argument reason being it is Unexpected token this Also typo @getElementsByname
. But as you are sendingthis
from theclick-handler
, you do not need that. You can grab the element using any other argument name thanthis
function checkNumbers(element) {
if (element != null && element.value.length == 10) {
element = element.replace(element, '');
}
}
function isAllowedSymbol(input) {
var value = input.value;
var rep = /[a-zA-Z]/;
var rep2 = /[а-яА-Я]/;
if (rep.test(value)) {
value = value.replace(rep, '');
input.value = value;
if (rep2.test(value)) {
value = value.replace(rep2, '');
input.value = value;
}
}
}
<input type="text" maxlength="10" onkeyup="isAllowedSymbol(this);checkNumbers(this); " placeholder="Enter data" name="answer">