-1

My application is in Arabic and English. Its a hybrid application (html/css/jquery/android). we have one text field in one page and it should take input only alphanumeric (i.e., abc123). i want to prevent input other than alphanumeric. ( some customer are using Arabic keyboard and they enter something else from Arabic keyboard. and here it showing question marks (?) in database.)

So I have to prevent input other than alphanumeric. Please suggest me how can i prevent to take input as arebic or anything other than alphanumeric. Thanks in advance.

this is my code...

function isAlphaNumeric(e){ 
 var k;
 document.all ? k=e.keycode : k=e.which;
 return((k>47 && k<58)||(k>64 && k<91)||(k>96 && k<123)||k==0);
 }
<input type="text"  OnKeypress="javascript:return isAlphaNumeric(event,this.value);" >
Gaurab Kumar
  • 2,144
  • 2
  • 17
  • 29

3 Answers3

0

Add this in the XML:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
albertoqa
  • 525
  • 3
  • 11
0

Since jquery is tagged I am giving this solution. I think you can use regular expression for this like the one below:

 $('#text1').keypress(function (e) {
        var regex = new RegExp("^[a-zA-Z0-9]+$");
        var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
        if (regex.test(str)) {
            return true;
        }
    
        e.preventDefault();
        return false;
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text1">
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • Hi guru, thanks for your comment, your code is working fine in computer's browser but its not working in mobile hybrid app. Thats why I am using keycode on keypress like this. (k>47 && k<58)||(k>64 && k<91)||(k>96 && k<123)||k==0); and its working partially fine... like we cant input special character but it takes rupees symbol and also it takes arabic letter. – Gaurab Kumar Jun 19 '15 at 07:50
  • @GaurabKumar.. You can try **[this solution](http://stackoverflow.com/a/10344887/2065039)** – Guruprasad J Rao Jun 19 '15 at 07:53
0

If you want to allow both caps and small

<EditText
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:digits="qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0123456789"/>
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20