TejSoft
TejSoft

Reputation: 3315

How to block Unicode characters from a text box input?

I want to block users from entering Unicode characters into any of the text boxes on a specific form on an web page. Is there a regular expression validation check I can use with jQuery to achieve this?

Upvotes: 2

Views: 3727

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626851

I think you can use [\x20-\x7E] regex to allow any ASCII characters between a space and the tilde symbol, like this:

$("#Your_Form_Id").submit(function() {
    var input_value = $("#Your_Text_Box_Id").val();
    var pattern = /^[\x20-\x7E]*$/;
    if(!pattern.test(input_value)) {
        $("#Your_Text_Box_Id").after('<span class="error">Only printable ASCII is allowed.</span>');
    }
});

Upvotes: 2

Related Questions