How to accept only characters to text area with Turkish characters?

900 Views Asked by At

here is my code for a textareainput to accept only characters not numbers.

var harfInput1 = document.getElementById('graduated_university_country');
    harfInput1.addEventListener("keyup",function(){
        if (harfInput1.value.match(/[^a-zA-Z' ']/g)){
            harfInput1.value = this.value.replace(/[^a-zA-Z]/g,'');
        }
    })

Problem is i cannot accept Turkish characters like this. I tried to add code below but it did not work.

var harfInput1 = document.getElementById('graduated_university_country');
    harfInput1.addEventListener("keyup",function(){
        if (harfInput1.value.match(/[^a-zA-Z' '][^\wığüşöçĞÜŞÖÇİ]/g)){
            harfInput1.value = this.value.replace(/[^a-zA-Z][^\wığüşöçĞÜŞÖÇİ]/g,'');
        }
    })

Any suggestions?

3

There are 3 best solutions below

0
oguzhancerit On BEST ANSWER

You can use below code snippet to catch all alpha letters including Turkish letters with Javascript using Regular expressions.

var harfInput1 = document.getElementById('graduated_university_country');
  harfInput1.addEventListener("keyup",function(){
  if (harfInput1.value.match(/[^a-zA-Z' 'wığüşöçĞÜŞÖÇİ]/g)){
      harfInput1.value = this.value.replace(/[^a-zA-ZwığüşöçĞÜŞÖÇİ]/g,'');
  }
})
<textarea name="" id="graduated_university_country" cols="" rows=""></textarea>

0
user14906787 On

I would suggest that you take a look at regular expressions, regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Once, you understand regex, try using the following regular expression:

[^A-Z^a-z^0-9^&#351;&#350;&#305;&#304;çÇöÖüÜ&#286;&#287;\s]
0
Henju On

Have you tried .charAt(). Check if input character falls inside the range of turkish unicode.