如何限制特殊字符和 ( /,*,+) only

how to restrict special characters and ( /,*,+) only

我们有一个文本 Field.We 知道如何限制特殊 characters.But 我们需要允许字母和数字以及连字符 (-) only.No 需要特殊字符,但 (-) 除外。给我任何想法。

我的代码:

$('#pduration').keydown(function (e) {
           if (e.shiftKey || e.ctrlKey || e.altKey) {
                e.preventDefault();
            } else {
               var key = e.keyCode;
               if (keyCodeEntered == 45) {
                   // Allow only 1 minus sign ('-')...
                  if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0))
                       return false;
                   else
                       return true;
               }

           }
       });

如果我们尝试此代码,它会限制特殊字符,但它允许 -、/、+ 请指导我只允许数字和字母以及连字符

替换此部分:

if (keyCodeEntered == 45) {
// Allow only 1 minus sign ('-')...
if ((elementRef.value) && (elementRef.value.indexOf('-') >= 0))
       return false;
{
else
     return true;
}

有了这个:

 //         keys a-z,0-9               numpad keys 0-9            minus sign    backspace
if ( ( key >= 48 && key <= 90 ) || ( key >= 96 && key <= 105 )  || key == 109 || key==8)
{
    //return true;
}
else
{
    //return false
}
})

使用正则表达式模式匹配非常容易。

对于JavaScript,我推荐https://regex101.com/,对于一般的正则表达式,我推荐Rubular 用于测试和学习。

正则表达式模式如下所示:

/pattern/flags

**首先,声明一个正则表达式模式*

/<regex here>/

为了只捕获特定类型的字符,我们将使用字符 classes。

/[<char class here]/

然后使用这个class来匹配第一个小写字母,第一个大写字母,第一个数字或第一个“-”字符。

/[a-zA-Z0-9-]/

这只会捕获第一个字符

因为我们想要所有匹配的字符,所以我们为 global 添加标志 g, 这将 return all 匹配的字符。获得所有 合法 标志的最终模式如下所示:

/[a-zA-Z0-9-]/g

模式就是这样。

为了检查某些内容是否包含非法字符,如您所问,您可以这样做(两个示例都有效):

function verifyIllegalCharacters (inputString) 
{
    // Copy the results from replace to new string
    // It now holds the original string, minus all legal characters.
    // Since they were overwritten by "".
    var newStr = inputString.replace(/[a-zA-Z0-9-]/g, "");

    // If length is 0, all legal characters were removed, 
    // and no illegal characters remain. 
    return (newStr.length == 0);
}

function verifyIllegalCharacters (inputString) 
{
    // Same, but here we instead check for characters
    // NOT matching the pattern. Above we capture all legal chars,
    // here we capture all illegal chars by adding a ^ inside the class,
    // And overwrite them with "".
    var newStr = inputString.replace(/[^a-zA-Z0-9-]/g, "");

    // If the lengths aren't equal, something was removed
    // If something was removed, the string contained illegal chars.
    // Returns true if no illegal chars, else false.
    return (newStr.length == inputString.length);
}

我已使用此代码,Alhamd ul Lillah 100% 有效。

<script type="text/javascript">
        /*  48-57 - (0-9) NUMBERS
            65-90 - (A-Z)
            97-122 - (a-z)
            8 - (BACKSPACE)
            32 - (SPACE)
            45 - '-' (MINUS, HYPHEN, DASH)
        */ // NOT ALLOW SPECIAL
        function blockSpecialKeys(e) {
            var Keys = e.keyCode;
                return ( 
                    ( Keys >= 65 && k <= 90 ) || // (A-Z)
                    ( Keys >= 97 && k <= 122 ) || // (a-z)
                    ( Keys == 8 ) || // (BACKSPACE)
                    ( Keys == 32 ) || // (SPACE)
                    ( Keys == 45 ) // '-' (MINUS, HYPHEN, DASH)
                    );
        } // END OF blockSpecialKeys FUNCTION
</script>
    <input type="text" ... remaining coding ... onKeyPress="return blockSpecialKeys(event);">

希望你也能从中受益!