0

I want my textbox to accept only Greek letters. I use this code but doesn't work.

JS:

<script language="javascript">
     document.logOn.onsubmit = validate;
     function validate() { 
         var name = document.logOn.pw.value;
         if(!name = " [A-ZA-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]") {
             alert("Δεν επιτρεπονται λατινικη χαρακτηρες!");
             return false;
         }
         return true;
     }
<script>

In HTML I use, onkeypress = validate()

bharadhwaj
  • 2,059
  • 22
  • 35
user8190974
  • 23
  • 1
  • 1
  • 6

6 Answers6

2

You will have to use string.match(regexp) for regex matching. Also, I believe that the regex should be /^[A-ZA-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]+$/

ArunGeorge
  • 495
  • 5
  • 11
0

This question has been asked a few times.

I'm not an expert on the Greek alphabet, but something like this seems to work:

(/^[Α-Ωα-ωίϊΐόάέύϋΰήώ]+$/).test('ιτρε'); // true
(/^[Α-Ωα-ωίϊΐόάέύϋΰήώ]+$/).test('ιτρεkalimera'); // false

So, in your condition:

if ( (/^[Α-Ωα-ωίϊΐόάέύϋΰήώ]+$/).test(name) ) {
   // you are speaking Greek to me
}
0

Your code does not use regex. !name = " [A-ZA-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]" mean name literally must be [A-ZA-zΑ-Ωα-ωίϊΐόάέύϋΰήώ].

Convert it to regex if (!name.test(/ [A-ZA-zΑ-Ωα-ωίϊΐόάέύϋΰήώ]+/)). .test returns true/false


Your regex would match {space}{any letter fro A to Z or from A to z or form A to Ω or α to ω or any of ίϊΐόάέύϋΰήώ}

Justinas
  • 41,402
  • 5
  • 66
  • 96
0

Try This.

<script type='text/javascript'>
 function validate()
{
    var name=document.getElementById('logOn').pw.value;
    if(!(new RegExp("[Α-Ωα-ωίϊΐόάέύϋΰήώ]").test(name)))
    {
        alert("Δεν επιτρεπονται λατινικη χαρακτηρες!");
        return false;
    }


    return true;

}
    window.onload = function() {   document.getElementById('logOn').onsubmit = validate; };

</script>

This will alert if the text contains letters other than that in RegEx.

  • Please Note: Op didn't ask for characters A-Z a-z in the text. – user2984602 Jul 13 '17 at 06:25
  • Still not working. As I mention in HTML I put the function in onkeypress – user8190974 Jul 13 '17 at 06:54
  • My bad wrote the answer in hurry. That was because of the statement document.logOn .Try It Now. – user2984602 Jul 13 '17 at 07:35
  • Thanks for help but doesnt work. . In my case I have a plain text that when I click enter appear an alert that mention my inncorrect inputs – user8190974 Jul 13 '17 at 07:50
  • What do you mean you have plain text Please improve you question – user2984602 Jul 13 '17 at 08:43
  • I have an input text and there is no button to save the value. When I press enter the value saved in database – user8190974 Jul 13 '17 at 09:22
  • I have an input text i want to say. Also when replace logOn with my input id and when i remove pw seems to work but when appear the alert continue to accept the incorrect inputs and also when i press any key in keyboard appears the alert – user8190974 Jul 13 '17 at 09:51
  • Give me an input which is not working for you, in the above code. I tried pressing enter the alert system works. For Me: I entered : Abc - popup I entered : Ω - no popup and also some other cases What input is not working for you man. Also what do u mean you press any key keyboard alert occurs ? Do You want that to happen or not.? appear the alert continue to accept the incorrect inputs ??? Dude please improve your grammar I am really unable to understand If you have your webpage hosted somewhere please provide a link. (* i dont know if its allowed in SO though *) – user2984602 Jul 14 '17 at 04:35
  • I dont want english letter. For example when i insert the letter e.g a the alert appears but the letter appear in the input text also – user8190974 Jul 14 '17 at 07:11
0

I believe that the problem is here

    document.logOn.onsubmit = validate;
     var name = document.logOn.pw.value;

Maybe I need to use something else. I suppose this works on submit input. In my case I have a plain text that when I click enter appear an alert that mention my inncorrect inputs. Any usefull advice is accepted

user8190974
  • 23
  • 1
  • 1
  • 6
0

The main problem that have been existed yet is that the alert is appeared but are accepted incorrect entries. I suppose that my jacascript is correct and the problem appear in my HTML code. I use onclick='return validate ()'

user8190974
  • 23
  • 1
  • 1
  • 6