I want to format an input, I do not know what to do: '(
I want the format to come out this way
00-00-0000-1234567
But it comes out like this
00-00-1234567-0000
<form id="form" method="post" action="">
<input id="serial" name="serial" type="text" maxlength="15" />
</form>
<script>
(function($, undefined) {
"use strict";
$(function() {
var $form = $( "#form" );
var $input = $form.find( "input" );
$input.on( "keyup", function( event ) {
var selection = window.getSelection().toString();
if ( selection !== '' ) {
return;
}
if ( $.inArray( event.keyCode, [38,40,37,39] ) !== -1 ) {
return;
}
var $this = $(this);
var input = $this.val();
input = input.replace(/[\W\s\._\-]+/g, '');
var split = 4;
var chunk = [];
for (var i = 0, len = input.length; i < len; i += split) {
split = ( i >= 4 && i <= 15 ) ? 7 : 2;
chunk.push( input.substr( i, split ) );
}
$this.val(function() {
return chunk.join("-").toUpperCase();
});
} );
$form.on( "submit", function( event ) {
var $this = $( this );
var arr = $this.serializeArray();
for (var i = 0; i < arr.length; i++) {
arr[i].value = arr[i].value.replace(/[($)\s\._\-]+/g, '');
};
console.log( arr );
event.preventDefault();
});
});
})(jQuery);
</script>