Skip to content Skip to sidebar Skip to footer

Regular Expression To Force Single Blank Space After A First Character

How do I force a space after first character as user types. I just need only one space after first character (no spaces allowed before first one). After first character and space t

Solution 1:

I don't know why you want to do this but to remove spaces you can use this :

var str = $(this).val().replace(" ", "");

And for add a space after first character :

var str = $(this).replace("^(.)(.*)$", "$1 $2");

Solution 2:

Do you need the regex?

$('.singleSpace').keyup(function() {
  var $el = $(this);
  varval = $el.val();
  if (val.length === 1) {
    $el.val(val + ' ');
  }
});

Solution 3:

The var is done to deal with the cursor position

var val="";
$('.singleSpace').keyup(function() {
    var ele = $(this)[0];
    var foo = ele.value;
    if(val==foo){returnfalse}
    var startPos = ele.selectionStart;
    var endPos = ele.selectionEnd;
    if(event.keyCode==8){
        var diff=endPos-startPos;
        startPos-=diff;
        endPos-=diff;
    }
    foo = foo.replace(newRegExp('( )','g'),'');
    foo = foo.replace(newRegExp('^ *(.) *(.*?)'),'$1 $2');
    val=foo;
    ele.value=foo;
    ele.selectionStart=startPos;
    ele.selectionEnd=endPos;
});

This is the regExp you want: *(.) *(.*?) and you want to use .replace not match

Demo

Solution 4:

You can use this code :

$('.singleSpace').keyup(function() {
    var foo = $(this).val().replace(/^( *)([^ ] *)(.*)/g, function(_,b,c,d){
        return $.trim(c) + ' ' + d.replace(/ /g, '').slice(0,40);
    })
    
    $(this).val(foo)
});

The only problem is the cursor position. If you type while the cursor is not at the end, the cursor will always return the the end.

See for yourself : http://jsfiddle.net/S3gJL/2/

I would suggest you tu put that function in a blur event instead, but that can be annoying aswell.

Your request is not simple at all. There is a lot of variation, users can copy paste, select and delete. It is hard to prevent everything.

The code provided above block a lot of detour (only thing that doesn't block is the right-click paste and maybe other things i'm not think of), but with the cost stated above. You might wanna search for a plugin or hire someone to develop a good system.


Edit 1

Remove the authorisation to write space after the first one.


Edit 2

For the cursor position here the fix :

$('.singleSpace').keyup(function() {
    var foo = this.value.replace(/^( *)([^ ] *)(.*)/g, function(a,b,c,d,e){
        return $.trim(c) + ' ' + d.replace(/ /g, '').slice(0,40);
    })
    
    var carretPos = doGetCaretPosition(this)
    
    carretPos += foo.length - this.value.lengththis.value = foo;
    
    setSelectionRange(this, carretPos, carretPos)
});


//Code taken from// http://stackoverflow.com/questions/17858174/set-cursor-to-specific-position-on-specific-line-in-a-textareafunctionsetSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    elseif (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

//Code taken from// http://stackoverflow.com/questions/2897155/get-cursor-position-in-characters-within-a-text-input-fieldfunctiondoGetCaretPosition (oField) {
    
    // Initializevar iCaretPos = 0;
    
    // IE Supportif (document.selection) {
        
        // Set focus on the element
        oField.focus ();
        
        // To get cursor position, get empty selection rangevar oSel = document.selection.createRange ();
        
        // Move selection start to 0 position
        oSel.moveStart ('character', -oField.value.length);
        
        // The caret position is selection length
        iCaretPos = oSel.text.length;
    }
    
    // Firefox supportelseif (oField.selectionStart || oField.selectionStart == '0')
        iCaretPos = oField.selectionStart;
    
    // Return resultsreturn (iCaretPos);
}

FIDDLE

Post a Comment for "Regular Expression To Force Single Blank Space After A First Character"