开发者

javascript validation - only allow one capital letter

I need to add some validation that will only allow one capital letter in a string that may include spaces. The capital letter can be anywhere in the string, but can only be used once or not at all.

I was going to incorporate the solution below as a separate rule, but I have this bit of validation and wondering if I can just tweak it to get the desired result:

// Validate Sentence Case
if(dataEntryCaseId.toString().match("4")){
    var newValue = toTitleCase(value);
    if(newValue != value){
        for(var x = 1, j = value.length; x < j; x++){
            if(value.charAt(x) != newValue.charAt(x)){
                valid = false;
                $("#text_10").attr({"value":$("#text_10").attr("value").replace(value.charAt(x), "")});
                finalVal = 开发者_高级运维finalVal.replace(value.charAt(x), "");
            }
        }
    }
}


if(!valid){
    for(var x = 0, j = styleNoteJsonData.styleGroupNote.length; x < j; x++){
        if(styleNoteJsonData.styleGroupNote[x].styleName == styleGroupName){
            alert(styleNoteJsonData.styleGroupNote[x].styleNote);           
            $(".styleNote").addClass("alertRed");
            SendErrorMessage(styleNoteJsonData.styleGroupNote[x].styleNote);                
        }
    }


"this is A way to do it with regex".match(/^[^A-Z]*[A-Z]?[^A-Z]*$/)

Regex breaks down like this...

start of string (^) followed by not capital letter ([^A-Z]) zero or more times (*) follow by optional (?) capital letter ([A-Z]) followed by not capital letter ([^A-Z]) zero or more times (*) followed by end of string ($)


EDIT: simpler method based on idea from @IAbstractDownvoteFactory's answer

var string = "This is a simple way to do it"

// match all capital letters and store in array x
var x = string.match(/[A-Z]/g)

// if x is null, or has length less than 2 then string is valid
if(!x || x.length < 2){
    // valid
} else {
    // not valid
}

Regex matches all capital letters, and returns an array of matches. The length of the array is how many capitals there are, so less than 2 returns true.


How about this:

var string = "A string";
if(string.split(/[A-Z]/).length <= 2) {
    // all good
}
else {
    // validation error
}

Split the string on capital letters. If the length is 2 then there is exactly one captial.


You can give something like this a try:

function checkCapitals(InputString)
{

    // Counter to track how many capital letters are present
    var howManyCapitals = 0;

    // Loop through the string
    for (i = 0; i < InputString.length; i++)
    {

        // Get each character of the string
        var character = InputString[i];

        // Check if the character is equal to its uppercase version and not a space
        if (character == character.toUpperCase() && character != ' ') {
         // If it was uppercase, add one to the uppercase counter
         howManyCapitals++;
        }

    }

        // Was there more than one capital letter?
        if (howManyCapitals > 1)
        {
             // Yes there was! Tell the user.
             alert("You have too many capital letters!");
             return false;
        }

}

I hope I was of some help.


Could you loop through each character to check if it is equal to ascii code 65 through 94?

var CharArr = "mystring".toCharArray();
var countCapsChars = 0;

for(var i =0;i<= CharArr.length;i++) {
if (CharArr[i].CharCodeAt(0) >= 65 && CharArr[i].CharCodeAt(0) <=94) {
  countCapsChars++;
}

if (countCapsChars == 1 || countCapsChars == 0){
//pas
}
else 
{
//fail
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜