jQuery validation plugin: accept only alphabetical characters?

前端 未结 6 2287
-上瘾入骨i
-上瘾入骨i 2020-11-29 02:37

I\'d like to use jQuery\'s validation plugin to validate a field that only accepts alphabetical characters, but there doesn\'t seem to be a defined rule for it. I\'ve search

6条回答
  •  粉色の甜心
    2020-11-29 03:14

     $('.AlphabetsOnly').keypress(function (e) {
            var regex = new RegExp(/^[a-zA-Z\s]+$/);
            var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
            if (regex.test(str)) {
                return true;
            }
            else {
                e.preventDefault();
                return false;
            }
        });
    

提交回复
热议问题