仅允许文本字段中包含罗马字符

时间:2016-04-06 11:18:43

标签: javascript jquery

我找到了一种方法,使网站上的所有文本框都只接受罗马字符。有没有简单的方法在全球范围内做到这一点。

提前致谢。

4 个答案:

答案 0 :(得分:1)

在现代浏览器中<input>接受名为pattern的属性。这允许限制给定字段中的有效字符。

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

对于所有其他浏览器,您可以通过jQuery找到所有表单字段,检查是否存在pattern - 属性,并根据给定字段的值进行检查。您也可以替换不允许的字符:

$('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,
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>

哦,你还想在服务器端验证表单输入。所有HTML或Javascript内容都不会阻止您网站的所有访问者提交破碎的内容。

答案 1 :(得分:0)

对于过滤掉非罗马字符的正则表达式,我将参考以下问题的标记答案:

How to detect non-roman characters in JS?

剧透:正则表达式为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, ""); });

现在您只需要对jQuery进行一些修补:

SELECT products.*,    
COUNT(*) as resultsCount,
(SELECT COUNT(*) FROM products_categories pc WHERE pc.product_id = products.id) as categoriesCount

FROM products
JOIN products_categories AS productsCategories
ON productsCategories.product_id= products.id
WHERE productsCategories.category_id IN (7, 15, 8, 1, 50)

GROUP BY products.id
HAVING resultsCount = categoriesCount
ORDER BY amount DESC #optional

答案 2 :(得分:0)

将此代码段包含在您的母版页中,例如:

<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>

答案 3 :(得分:0)

这是我基于@fboes答案的想法。 我还需要向用户显示出了什么问题,所以当连续输入几个禁止的字符时,会显示错误消息,但是没有冗余。

  //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));
    }
});