简体   繁体   中英

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

We have one text Field.We know how to restrict special characters.But We need Allow alphabet and Numbers and hyphen(-) only.No need Sepcial characters but except (-) . Give me any idea.

Mycode:

$('#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 we tried this code it's restrict spectal charecters but it's allow -,/,+ Please guide me only allow number and alphabet and hyphen only

replace this section:

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

with this:

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

It's quite easy to do with regex pattern matching.

For JavaScript I recommend https://regex101.com/ , and for regex in general i recommend Rubular for testing and learning.

A Regex pattern consists looks like this:

/pattern/flags

**First, declare a regex pattern*

/<regex here>/

In order to capture only certain types of characters, we'll use character classes.

/[<char class here]/

Then use this class to match first lowercase letter, first uppercase letter, first number or first "-" character.

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

This will only catch the first character

Since we want all matching characters, we add the flag g for global , which will return all the characters that match. A final pattern for getting all legal flags loosk like this:

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

That's it for the pattern.

In order to check if something contains illegal characters, like you asked, you can do something like this (both examples work):

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

I have used this code and Alhamd ul Lillah it is working 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);">

I hope you also will get it beneficial!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM