1

I'am using the following Regex Validation in order enter alpha only (Text Only values) to the text field. The regex validation works in pc environment, but hosted and access through an Android device through chrome app, the validation doesn't work (Users can enter symbols as well as numbers). Appreciate your valuable help.

The Method I tried

<input type="text" id="name1"   placeholder="Name" class="form-control" />

$('#name1').bind('keypress', alphaOnly);

function alphaOnly(event) {
    var value = String.fromCharCode(event.which);
    var pattern = new RegExp(/[a-zA-Z]/i);
    return pattern.test(value);
 }
cs-87
  • 186
  • 1
  • 6
  • 18

2 Answers2

1

You've just missed anchors:

var pattern = /^[a-zA-Z]+$/i;
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Thank you I have tried the same, the issue is that validation wont applies when accessing through Android device using chrome app, users can enter anything they like.. Also "keypress" attribute is working when comes to mobile ?? – cs-87 Jan 13 '20 at 11:49
1

Try this for mobile devices:

$('#name1').bind('keyup', alphaOnly);

function alphaOnly() {
    console.log(this.value);
    this.value = this.value.replace(/[^a-z]/gi,'');
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="name1"   placeholder="Name" class="form-control" />

Got idea from this answer

Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30
  • Thank you very much, it almost works, but a small problem, the enterd value duplicates inside the text box (eg Jhon entered and then Click '$' result JhonJohn) when click the symbols some time – cs-87 Jan 13 '20 at 12:47
  • No i can't reproduce error, sorry. This works fine in my mobile chrome browser – Ritesh Khandekar Jan 13 '20 at 12:51