javascript 验证 - 只允许一个大写字母

发布于 2024-12-04 00:15:45 字数 1043 浏览 2 评论 0原文

我需要添加一些验证,仅允许字符串中可能包含空格的一个大写字母。大写字母可以出现在字符串中的任何位置,但只能使用一次或根本不使用。

我打算将下面的解决方案合并为单独的规则,但我进行了一些验证,并想知道是否可以调整它以获得所需的结果:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妳是的陽光 2024-12-11 00:15:45
"this is A way to do it with regex".match(/^[^A-Z]*[A-Z]?[^A-Z]*$/)

正则表达式像这样分解...

字符串开头 (^) 后跟非大写字母 ([^AZ]) 零次或多次 (*) 后跟可选 (?) 大写字母 ([AZ]) 后跟非大写字母 ([^AZ]) 零或更多次 (*) 后跟字符串结尾($)


编辑:基于 @IAbstractDownvoteFactory 的答案的更简单的方法

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
}

正则表达式匹配所有大写字母,并返回匹配数组。数组的长度就是有多少个大写字母,因此小于 2 返回 true。

"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.

月隐月明月朦胧 2024-12-11 00:15:45

怎么样:

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

将字符串拆分为大写字母。如果长度为 2,则只有 1 个大写字母。

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.

欢烬 2024-12-11 00:15:45

你可以尝试这样的事情:

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

}

我希望我能有所帮助。

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.

—━☆沉默づ 2024-12-11 00:15:45

你能循环遍历每个字符来检查它是否等于 ascii 代码 65 到 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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文