Home > Software design >  Regex for allowing words which have only first letter as uppercase?
Regex for allowing words which have only first letter as uppercase?

Time:05-07

I am trying to make a regex which will validate if all the words have their first letter as upper case. ex like Fruits is allowed but fruits is not allowed. also United States is allowed but United states is not allowed.

also, i don't want to allow only spaces and dots.

i have created the regex for not allowing only space and dots separately but i want them to be in one regex with the upper requirement is their any way to achieve this ??

I'll add what i have tried so far.

please, any help is appreciated.


    $.validator.addMethod("first_letter", function(value,element)
    {
        return this.optional(element) || ^[A-Z].*i.test(value);  // 1.) this is what i am trying to check if first letter of every word is uppercase or not  and can this be combined with the 2.) validation i.e "characters" 
    }, "First Letter Should be Capital !!");

    $.validator.addMethod("characters", function(value,element)
    {
        return this.optional(element) || /^[a-zA-Z.*\s]*$/i.test(value);  // 2.) For only Allowing Alphabets
    }, "Only Characters are Allowed !!");
    
    $.validator.addMethod("dot", function(value,element)
    {
        return this.optional(element) || /^[^.]*$/i.test(value);  // 3.) For Not Allowing Dot Characters
    }, "Dot chatarcters are not Allowed !!");
    
    $.validator.addMethod("only_space", function(value,element)
    {
        return this.optional(element) || /^[.*\S.*]/i.test(value); // 4.) For Not Allowing Only Spaces
    }, "Only Spaces are not Allowed !!");

All the validations

    $.validator.addMethod("characters", function(value,element)
    {
        return this.optional(element) || /^[a-zA-Z.*\s]*$/i.test(value);
    }, "Only Characters are Allowed !!");
    
    $.validator.addMethod("dot", function(value,element)
    {
        return this.optional(element) || /^[^.]*$/i.test(value);
    }, "Dot chatarcters are not Allowed !!");
    
    $.validator.addMethod("only_space", function(value,element)
    {
        return this.optional(element) || /^[.*\S.*]/i.test(value);
    }, "Only Spaces are not Allowed !!"); 

    $.validator.addMethod("first_letter", function(value,element)
    {
        return this.optional(element) || ^[A-Z][a-z]*(?: [A-Z][a-z]*)*$/ i.test(value);
    }, "First Letter Should be Capital !!");    // your answer , if i am using it as it is then also its not working and other validations above it are also not working.

$.validator.addMethod("characters", function(value,element)
    {
        return this.optional(element) || /^[A-Z][a-zA-Z.*\s] (?: [A-Z][a-z]*)*$/i.test(value);
    }, "Only Characters are Allowed !!");    // what i tried 

$.validator.addMethod("first_letter", function(value,element)
    {
        return this.optional(element) || /^([A-Z] [a-z] * (([A-Za-z]\s)?[a-z]*)*)(\s \S \s*)*$/i.test(value);
    }, "First Letter Should be Capital !!");    // I also tried like this.

CodePudding user response:

A pattern like that can look like:

^[A-Z][a-z]*(?: [A-Z][a-z]*)*$

Explanation

  • ^ Start of string
  • [A-Z][a-z]* Match a single uppercase char and optional lowercase chars
  • (?: [A-Z][a-z]*)* Optionally repeat matching a space and the same as the previous
  • $ End of string

This pattern is limiter to characters A-Z as the first characters and followed by lowercase characters where there can be a mere space in between the "words".

See a demo on regex101.

const regex = /^[A-Z][a-z]*(?: [A-Z][a-z]*)*$/;
[
  "Fruits",
  "fruits",
  "United States",
  "United states"
].forEach(s => console.log(`${s} --> ${regex.test(s)}`));

A broader match could be starting with an uppercase char, followed by optional non whitespace characters (that can also again match uppercase characters).

In the repeating part you can use \s to match 1 or more whitespace characters (that can also match a newline).

^[A-Z]\S*(?:\s [A-Z]\S*)*$

See another demo on regex101.

  • Related