How to allow the user to enter login only in English?

    4 answers 4

    That's about

    if (!preg_match("#^[a-z0-9]+$#i", $field)) { die('Разрешены только символы az и цифры'); } 

      1) write about it clearly, in bright letters :)

      2) hang a script on the client that checks the field (SCHEMATICALLY):

       <input name="login" type="text" onkeyup="check_login()"> ... function check_login() { var frm = doc.getElementById('loginform'); if( !valid_login(frm.login.value) ) { alert( 'Низя!' ); } /* а можно, например, переводить не-ASCII символы в транслит */ } 

      3) check on the server side to do, wrote above

      4) to think: is it necessary? The length of the login is also checked OK, spaces are there, some reserved logins, but the letters are worse than this can only be password restrictions :(

      • Since you proposed in the second paragraph, it is better not to do it! - spoilt
      • Well not so, and, for example, to check on onsubmit () - it does not matter. The very idea of ​​restricting a login with a set of Latin is generally foolish :) - user6550

      Not at all :)

      You can write javascript that will handle the keystroke on the input field (c jQuery):

       $('#input').keydown( function(e) { if (!isSymbolAllowed(e)) //isSymbolAllowed проверяет разрешён ли символ e return false; //если не разрешён, то запрещаем этот символ return true; }); 

      This can be called client validation .

      But even this will not save you. The user can edit the HTML code of the page manually, and any information entered by him can come to your server. Therefore it is necessary to carry out also server validation .

        if (! preg_match ("# ^ [a-zA-z0-9] + $ # i", $ field)) {die ('Only az characters and numbers are allowed'); } This script, incomplete, lacks large English letters. Although I may be wrong, I didn’t understand how to make a pattern for a mask.

        • Kostya Samsin, the easiest and most correct: [a-zA-Z]. PS And how to send a comment to someone else's answer? - Pavlus