0

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?

Allan Wind
  • 7,923
  • 3
  • 20
  • 30
  • This [Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) might be of some help. – Huzaifa Dec 29 '20 at 11:21
  • Your revised code uses two character classes, requiring two characters, one from each of them, side-by-side. You want a single character class: `/[^a-zA-Z' '\wığüşöçĞÜŞÖÇİ]/g` Also note that `\w` includes `a-zA-Z` so that part becomes redundant, as is the second `'`. You probably want the `i` flag as well, and you don't need or want the `g` flag: `/[^' \wığüşöçĞÜŞÖÇİ]/i`. Separately: When you just want to know whether something matches and you don't use the result of the match, use the `test` method rather than `match`: `/[^' \wığüşöçĞÜŞÖÇİ]/i.test(harfInput1.value)` – T.J. Crowder Dec 29 '20 at 11:23
  • this worked perfectly for turkish chars but it also allows me input numbers i will try again i am begginer at js thanks for the help :) – Elif Bahar Özdoğru Dec 29 '20 at 11:40

3 Answers3

0

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>
oguzhancerit
  • 1,251
  • 1
  • 11
  • 22
-1

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

Henju
  • 1
  • 3
-3

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]