简体   繁体   中英

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 . They should be only input 2 string 9 numbers.

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM