How to create validation with 2 uppercase letter and number?

Viewed 158

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); 
});
3 Answers

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="[A-Z]{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
    //...      
});
Related