I have seen a lot of functions that format telephone or number (comma and decimals) in stackflow community like this question [here](https://stackoverflow.com/questions/16598470/javascript-regular-expression-to-add-dash-after-every-3rd-and-4th-characters) and others. Here's what I want to:

**Step 1: Maintain a Library for patterns like this:**

```
var library = {
    fullDate : {
        pattern : /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/,
        error : "Invalid Date format. Use YYYY-MM-DD format."
    },
    fullDateTime : {
        pattern : /^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}$/,
        error : "Invalid DateTime format. Use YYYY-MM-DD HH:MM (24-hour) format."
    },
    tel : {
        pattern : /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
        error : "Invalid Telephone format."
    }
};

```

**Step 2: Automatically add a character as they type. For exapmple, add a `-` after 4 numbers in Date.**

I have a text field say:

```
<input type="text" data-validate="fullDate" placeholder="YYYY-MM-DD"/>

```

And possible place to start script as:

```
$('body').on('keyup','input',function(){
   var validate = $(this).data('validate');
   var pattern = library[validate].pattern;

    //Some more steps here....

});

```

But, I cannot make any further because I am new to RegExp. Here's a **[startup fiddle](http://jsfiddle.net/tpahadi/664tgq27/)**. Anyone?

Further Notes: I have been able to validate using the following functions but what I want to is automatically make pattern:

```
function validate(libraryItem, subject){
    var item = library[libraryItem];
    if(item !== undefined){
        var pattern = item.pattern;
        if(validatePattern(pattern, subject)){
            return true;
        } else {
            return item.error;
        }
    }
    return false;
}

function validatePattern(pattern, subject){
    return pattern.test(subject);
}

```