Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
    <input type = "text" id = "foo" />
</body>
</html>
 
var patron1 = /[A-Z]/, 
    patron2 = /\d/,
    patron3 = /[A-Z]{1}\d{3}/gi,
    caracter;
document.querySelector("#foo").addEventListener("keypress", function(event){
    caracter = String.fromCharCode(event.keyCode);
    
    if ((patron3.test(this.value) && this.value.length == 4) || this.value.length == 4){
        this.value += "-";
    }
    
    if ((!this.value.length && !patron1.test(caracter)) || 
        (patron1.test(this.value) && !patron2.test(caracter)) || 
        this.value.length == 16){
        event.preventDefault();
    }
}, false);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
Alexis88pro
0viewers