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);
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正则表达式像这样分解...
字符串开头 (
^
) 后跟非大写字母 ([^AZ]
) 零次或多次 (*) 后跟可选 (
?
) 大写字母 ([AZ]
) 后跟非大写字母 ([^AZ]
) 零或更多次 (*
) 后跟字符串结尾($
)编辑:基于 @IAbstractDownvoteFactory 的答案的更简单的方法
正则表达式匹配所有大写字母,并返回匹配数组。数组的长度就是有多少个大写字母,因此小于 2 返回 true。
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.
怎么样:
将字符串拆分为大写字母。如果长度为 2,则只有 1 个大写字母。
How about this:
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:
I hope I was of some help.
你能循环遍历每个字符来检查它是否等于 ascii 代码 65 到 94 吗?
Could you loop through each character to check if it is equal to ascii code 65 through 94?