简体   繁体   English

如何使用 2 个大写字母和数字创建验证?

[英]How to create validation with 2 uppercase letter and number?

Assuming this is the student id CC201801194 , I want to restrict from inputting lowercase like this cc20181194 .假设这是学生 ID CC201801194 ,我想限制输入像这样的小写字母cc20181194 They should be only input 2 string 9 numbers.它们应该只输入 2 个字符串 9 个数字。

This is what I have got:这就是我得到的:

$.validator.addMethod("noCaps", function(value, element) {
   return this.optional(element) || /[A-Z]{2}/.test(value); 
});

Can be achieved straight in the HTML markup (without any JS) using the pattern attribute .可以使用模式属性在 HTML 标记(没有任何 JS)中直接实现。

<input type="text" pattern="[A-Z]{2}[0-9]{9}">

 input + span:after { position: absolute; padding-left: 5px; } p.hint { display: none; } input:valid + span:after { content: "✓"; } input:placeholder-shown + span:after{ content: ""; } input:invalid + span:after { content: "✖"; } input:invalid + span + p.hint { display: block; }
 <form> <input type="text" pattern="[AZ]{2}[0-9]{9}" Placeholder="Student ID"> <span class="validity"></span> <p class="hint">Student ID must be 2 capitals and 9 numbers.</p> </form>

Try this:尝试这个:

$.validator.addMethod("noCaps", function(value, element) {
   return this.optional(element) || /[A-Z]{2}[0-9]{9}/.test(value); 
});

Try this:尝试这个:

$("#...").keydown(function(e) {
    var test = /[A-Z]{2}[0-9]{9}/; //regex
    //...      
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用大写和数字的正则表达式验证匹配密码并确认密码 - How to match password and confirm password with regex validation for uppercase and number 密码生成 function 至少有1个数字,1个大写字母和1个小写字母 - Password generation function with at least 1 number, 1 uppercase letter and 1 lowercase letter 如何在angularjs中匹配大写和小写字母? - How to match uppercase and lower case letter in angularjs? 如何在单击按钮时将首字母设置为大写 - How to set first letter to upperCase on button click jQuery验证 - 密码必须包含大写和数字 - jQuery Validation - password MUST contain uppercase and number Parsley 自定义验证器 - 带数字、大写字母和特殊字符的密码 - Parsley custom validator - password with number, uppercase letter and special character 正则表达式不起作用-至少8个字符,1个数字,1个特殊字符和1个大写字母 - Regex not working - min 8 character, 1 Number, 1 special character and 1 uppercase letter javascript 至少一个数字和一个大写字母的正则表达式 - javascript regular expression for atleast one number and one uppercase letter 以字母开头的正则表达式包含一个大写/一个小写字母,一个数字,没有特殊字符和最少8个字符 - Regex that starts with letter, contains one uppercase/one lowercase letter, one number and no special characters & min 8 characters 用于匹配 1 个字符大写字母的正则表达式,第二个可以是字母/数字,rest 数字 - Regex for matching 1 character a uppercase letter, 2nd could be letter / number, rest numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM