簡體   English   中英

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

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

我們有一個文本字段。我們知道如何限制特殊字符。但我們只需要允許字母和數字和連字符 (-)。不需要特殊字符,但 (-) 除外。 給我任何想法。

我的代碼:

$('#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>/

為了僅捕獲某些類型的字符,我們將使用字符類。

/[<char class here]/

然后使用這個類來匹配第一個小寫字母、第一個大寫字母、第一個數字或第一個“-”字符。

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

這只會捕獲第一個字符

由於我們想要所有匹配的字符,我們為global添加標志g ,它將返回所有匹配的字符。 獲取所有合法標志的最終模式如下所示:

/[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);">

我希望你也能從中受益!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM