2011-09-09 58 views
1

我需要添加一些驗證,只允許一個字符串中可能包含空格的大寫字母。大寫字母可以在字符串中的任何位置,但只能使用一次或根本不使用。javascript驗證 - 只允許一個大寫字母

我要結合以下作爲一個單獨的規則的解決方案,但我已經驗證這一點,不知道如果我可以調整它以獲得期望的結果:

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

回答

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

正則表達式打破了這樣的...其次是不大寫字母串(^)([^A-Z])零次或多次(*)通過可選(?)大寫字母遵循

開始([A-Z]其次)通過不大寫字母后面的字符串($


結束( [^A-Z])零次或多次( *

編輯:基於想法從@ 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。

+0

+1我不認爲你可以在正則表達式中做到這一點。非貪心......你是最偉大的。 –

+0

你願意添加更多的上下文嗎?例如,如果!= .match(/^[^ A-Z] * [A-Z]?[^ A-Z] * $ /);警覺(太多的首都); - 基本用法是? – Jason

+0

如果你喜歡我的回答,你可以投給哥哥upvote:P – Joe

1

你可以給像這是一個嘗試:

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

} 

我希望我有一些幫助。

0

你可以遍歷每個字符來檢查它是否等於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 
} 
2

如何:

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

斯普利特大寫字母串。如果長度是2,那麼只有一個資本。

+0

有趣的做法。比較簡單 – Jason