Javascript验证-只允许一个大写字母
javascript validation - only allow one capital letter
我需要添加一些验证,在可能包含空格的字符串中只允许一个大写字母。大写字母可以出现在字符串的任何位置,但只能使用一次,或者根本不使用。
我打算将下面的解决方案合并为一个单独的规则,但我有这一点验证,想知道我是否可以调整它以获得所需的结果:
// 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]*$/)
正则表达式解析如下:
字符串的开始(^
)后跟非大写字母([^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
}
Regex匹配所有大写字母,并返回一个匹配数组。数组的长度是大写字母的个数,所以小于2返回true
这个怎么样:
var string = "A string";
if(string.split(/[A-Z]/).length <= 2) {
// all good
}
else {
// validation error
}
用大写字母分割字符串。如果长度为2,则只有一个大写字母
你可以试一下:
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;
}
}
希望我能帮到你。
你能遍历每个字符来检查它是否等于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
}
相关文章:
- 有没有一个函数会忽略javascript中的大写字母
- 如何使用至少一个数字、一个大写字母和6-20个字符验证密码
- Javascript正则表达式,需要一个大写字母、一个小写字母和一个数字,并允许特殊
- 编写一个包含一个大写字母和六个数字的javascript表达式
- JavaScript-删除最后一个大写字母后的所有字符
- 生成一个随机数字序列,然后用JavaScript在该序列后面附加一个大写字母
- 密码的正则表达式必须包含至少八个字符,至少一个数字以及小写和大写字母以及特殊字符
- 如何创建一个在字符串中查找大写字母并添加空格的函数
- 正则表达式至少包含 10 个字符,包括 1 个数字和一个大写字母
- 至少一个数字和一个大写字母的 JavaScript 正则表达式
- Sequelize在插入一个大写字母时返回小写字母
- 从最后一个大写字母开始剪字符串直到结尾
- 正则表达式查找至少有一个大写字母的子字符串
- 正则表达式,用于匹配九个数字和末尾的一个大写字母
- Javascript验证-只允许一个大写字母
- 匹配至少一个大写字母、至少一个数字和任意数量的特殊字符的正则表达式
- 一个大写字母的Javascript正则表达式
- Javascript 正则表达式表示“至少 X 个字符,有一个大写字母和一个数字”
- 在一个块中以所有大写字母显示字母
- Jquery或javascript密码生成器,至少有一个大写字母和一个数字