0

I am finding a way to make all the text boxes in the website only accept roman characters. Is there any easy way to do it globally.

Thanks in advance.

Gaurav Aggarwal
  • 9,809
  • 6
  • 36
  • 74
Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35
  • 2
    It is expected that you at least attempt to code this for yourself. Stack Overflow is not a code writing service. I would suggest that you do some additional research, either via Google or by searching SO, make an attempt and. if you still have trouble, come back with **your code** and explain what you have tried and why it did not work. – Paulie_D Apr 06 '16 at 11:22
  • What had you tried – Zafta Apr 06 '16 at 11:24
  • 1
    See: [Latin Characters check](http://stackoverflow.com/questions/15785508/latin-characters-check). It's not as simple as it might seem. – Yogi Apr 06 '16 at 11:29
  • @Paulie_D I am nt asking for code here. I know how to implement it on a single text box but I want to do it globally on all text box in the application without breaking any other functionality. I am looking just for an idea. – Venkata K. C. Tata Apr 06 '16 at 11:35

4 Answers4

3

In modern browsers <input> accepts an attribute called pattern. This allows to restrict the valid characters in a given field.

input:invalid {
  background-color:red;
}
<form>
  <input type="text" pattern="[a-zA-Z\s\.\-_]+" />
  <button type="submit">Submit</button>
</form>

For all other browsers you can find all form field via jQuery, check if a pattern-attribute exists, and check it against the value of a given field. You may also replace disallowed characters:

$('form').on('keyup blur','input',function() {
  if ($(this).val() && $(this).attr('pattern')) {
    //var pattern = new RegExp('^'+$(this).attr('pattern')+'$', 'g');
    //$(this).toggleClass('invalid', pattern.match(!$(this).val()));
    var pattern = new RegExp($(this).attr('pattern').replace(/\[/,'[^'), 'g');
    $(this).val($(this).val().replace(pattern,''));
  }
});
input:invalid {
  background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<form>
  <input type="text" pattern="[a-zA-Z\s\.\-_]+" />
  <button type="submit">Submit</button>
</form>

Oh, you still want to validate form inputs on the server-side. All HTML- or Javascript-stuff does not prevent all visitors of your site to submit broken stuff.

Aerogirl
  • 369
  • 4
  • 17
fboes
  • 2,149
  • 16
  • 17
  • If I were the OP I would mark this answer as the correct one. – Geeky Guy Apr 06 '16 at 12:28
  • 2
    Not really a solution as this doesn't prevent the user from actually entering non-Latin characters like "Сергей" (Sergei). It does highlight the characters as invalid, but it also marks many legitimate Latin characters (Polish, Spanish, Italian, etc) as invalid. For example, how would someone enter a German street name, e.g., straße – Yogi Apr 06 '16 at 12:45
  • You are completely right, this only accepts roman characters as in ASCII. If you need more characters, refer to the solution of @Renan – fboes Apr 07 '16 at 08:30
  • Oh, I fixed the JS to remove invalid characters – fboes Apr 07 '16 at 08:35
1

I will refer to the marked answer for the following question for the regex which filters out non-roman characters:

How to detect non-roman characters in JS?

Spoiler: the regex is /[^\u0000-\u024F\u1E00-\u1EFF\u2C60-\u2C7F\uA720-\uA7FF]/g

Now all you need is a little bit of tinkering with jQuery:

var myInputId = "#foo"; // Or whatever you wish to use.
var input = $(myInputId);
var exp = /[^\u0000-\u024F\u1E00-\u1EFF\u2C60-\u2C7F\uA720-\uA7FF]/g;
input.blur(function() {
    input.value = input.value.replace(exp, "");
});
Community
  • 1
  • 1
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
0

Include this snippet into your master page for example:

<script>
    $(function(){
        $('input[type=text],textarea').keypress(function(e){
            var char = String.fromCharCode(e.which || e.charCode); 
            var rgx = /[\u0000-\u007F]/;
            if (rgx.test(char) == false)
                return false;
          })
    })
</script>
tenbits
  • 7,568
  • 5
  • 34
  • 53
0

Here is my idea based on @fboes answer. I also needed to show user whats wrong, so there is error message showing but with no redundancy when typing couple of forbidden characters in a row.

  //I wanted first to assign pattern attr to every input in form but when it's happening, all "\" chars are removed from regex therefore - it doesn't work, so I had to add it in templates for every input.

let isIncorrect = false;

scope.checkPattern = function(e) {

    // I don't want to allow Chineese, cyrylic chars but some other special - yes
    var pattern = new RegExp('[a-zA-Z\s\.\-_äÄöÖüÜßąćęłńóśźżĄĆĘŁŃÓŚŹŻ]+', "g");
    if ($(e).is(':valid')){
        return true
    } else {
        $(e).val($(e).val().replace(pattern,''));
        return false
    }
};

scope.removeAlert = function (e){
    $(e).parent().find('.text-danger').remove();
    isIncorrect = false;
}

// unallowed characters in order inputs
$('.my-form').on('keyup blur','input',function(e) {

    if (!scope.checkPattern($(this))) {
        if (!isIncorrect){
            // show this error message but only once (!) and for specified period of time
            $(this).parent().append('<p class="text-danger">Only latin characters allowed</p>');
            isIncorrect = true;
        }

        setTimeout(scope.removeAlert, 3000, $(this));
    }
});
Aerogirl
  • 369
  • 4
  • 17