0

i have javascript that validate every user input and only allow character to be inputed in input text.

my issue is im using input text with no submit button (search form). my javascript doesn't allow hotkey enter, here my code :

function getkey(e)
{
if (window.event)
   return window.event.keyCode;
else if (e)
   return e.which;
else
   return null;
}
function angkadanhuruf(e, goods, field)
{
var angka, karakterangka;
angka = getkey(e);
if (angka == null) return true;
 
karakterangka = String.fromCharCode(angka);
karakterangka = karakterangka.toLowerCase();
goods = goods.toLowerCase();
 
// check goodkeys
if (goods.indexOf(karakterangka) != -1)
    return true;
// control angka
if ( angka==null || angka==0 || angka==8 || angka==9 || angka==27 )
   return true;
    
if (angka == 13) {
    var i;
    for (i = 0; i < field.form.elements.length; i++)
        if (field == field.form.elements[i])
            break;
    i = (i + 1) % field.form.elements.length;
    field.form.elements[i].focus();
    return false;
    };
// else return false
return false;
}
<div class="search-box" style="margin-top: 20px;">
            <form method="POST" id="searchform" action="search.html" >
          <input name="kata" type="text" placeholder=" "  onKeyPress="return angkadanhuruf(event,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_',this)"/>

          </form>
            </div> 

2
  • found this stackoverflow.com/questions/20484738/… Commented Dec 16, 2020 at 4:05
  • Hi @SSpoke, already try it but doesnt work.. in my code only allow character that declared on attribut onkeypress Commented Dec 16, 2020 at 6:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.