Regular expression to accept only characters (a-z) in a textbox

后端 未结 8 1379
北荒
北荒 2021-01-05 12:16

What is a regular expression that accepts only characters ranging from a to z?

相关标签:
8条回答
  • 2021-01-05 12:48

    Just for people using bash shell, instead of "+" use "*"

    "^[a-zA-Z]*$"

    0 讨论(0)
  • 2021-01-05 12:52

    ^[A-Za-z]+$ To understand how to use it in a function to validate text, see this example

    0 讨论(0)
  • 2021-01-05 13:00

    The pattern itself would be [a-z] for single character and ^[a-z]+$ for entire line. If you want to allow uppercase as well, make it [a-zA-Z] or ^[a-zA-Z]+$

    0 讨论(0)
  • 2021-01-05 13:01

    Try to use this plugin for masking input...you can also check out the demo and use this plugin if this is what you may want...

    Masked Input Plugin

    As you can see in the demonstration that you can use both alphatbets and numbers in a combination for complex textbox validations where an user might want to type not only alphatbets(azAZ) but also with numbers too(ie. alphanumberics)...specific validations like accepting only numbers in particular format(eg.phone numbers) can be done...that is the case when you can use this plugin for different circumstances..

    hope this helps...

    0 讨论(0)
  • 2021-01-05 13:03

    Allowing only character and space in between words :

    ^[a-zA-Z_ ]*$

    Regular Expression Library

    0 讨论(0)
  • 2021-01-05 13:05

    None of the answers exclude special characters... Here is regex to ONLY allow letters, lowercase and uppercase.

    /^[_A-zA-Z]*((-|\s)*[_A-zA-Z])*$/g
    

    And as for different languages, you can use this function to convert letters to english letters before the check, just replace returnString.replace() with letters you need.

    export function convertString(phrase: string) {
        var maxLength = 100;
    
        var returnString = phrase.toLowerCase();
        //Convert Characters
        returnString = returnString.replace("ą", "a");
        returnString = returnString.replace("č", "c");
        returnString = returnString.replace("ę", "e");
        returnString = returnString.replace("ė", "e");
        returnString = returnString.replace("į", "i");
        returnString = returnString.replace("š", "s");
        returnString = returnString.replace("ų", "u");
        returnString = returnString.replace("ū", "u");
        returnString = returnString.replace("ž", "z");
    
        // if there are other invalid chars, convert them into blank spaces
        returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
        // convert multiple spaces and hyphens into one space
        returnString = returnString.replace(/[\s-]+/g, " ");
        // trims current string
        returnString = returnString.replace(/^\s+|\s+$/g, "");
        // cuts string (if too long)
        if (returnString.length > maxLength) returnString = returnString.substring(0, maxLength);
        // add hyphens
        returnString = returnString.replace(/\s/g, "-");
    
        return returnString;
    }
    

    Usage:

    const firstName = convertString(values.firstName);
    
                if (!firstName.match(allowLettersOnly)) {
                }
    
    0 讨论(0)
提交回复
热议问题