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