开发者

Javascript to remove spaces from a textbox value

开发者 https://www.devze.com 2023-01-20 19:49 出处:网络
I\'ve searched around for this a lot and can\'t find a specific example of what I\'m trying to do. Basically I want to get the value of a textbox, by the name of the text box (not id). Then I want to

I've searched around for this a lot and can't find a specific example of what I'm trying to do. Basically I want to get the value of a textbox, by the name of the text box (not id). Then I want to remove ALL spaces, not just leading or trailing, but spaces in the text too. For example f开发者_高级运维or this html code:

<INPUT style="TEXT-ALIGN: right;" onkeyup="fieldEdit();" onchange="fieldChange();" NAME="10010input" VALUE="4 376,73" readonly >

I have this javascript:

var val = document.CashSheet.elements["10010input"].value; 
val2 = val.replace(<someregex>, '');
alert(val2);

But I've tried many available regex expressions that remove all spaces, but all only seem to remove leading and trailing spaces, for example the value above 4 376,73 should read as 4376,73 in the alert above but it doesn't. After looking into this normal regex for removing spacesfrom a text field contents is not working and I believe its because its being populated in Polish localised settings on the web server. What \u char/regex exp do I need to capture for a "Polish space" so to speak, in ascii it comes up as 20 but the regex exps that most people are suggesting for spaces does not capture it.


You can use document.getElementsByName to get hold of the element without needing to go through the form, so long as no other element in the page has the same name. To replace all the spaces, just use a regular expression with the global flag set in the element value's replace() method:

var el = document.getElementsByName("10010input")[0];
var val = el.value.replace(/\s/g, "");
alert(val);


You need to "generalize" that regexp you're using so it's applied to all matches instead of just the first. Like this:

val = val.replace(/\s/g, '')

Notice the 'g' that modifies the regexp so it becomes "general".


Here is a function I use to replace spaces.

function removeSpaces(val) {
   return val.split(' ').join('');
}


Another, more jQuery'ish option:

$(".stripspaces").keyup(function() {
    $(this).val($(this).val().replace(/\s/g, ""));
});


Try this: you can try to write or copy paste any string including white space, it's removed automatically.

/* Not Allow Spcace Type in textbox */
function AvoidSpace(event) {
    var k = event ? event.which : window.event.keyCode;
    if (k == 32) return false;
}

/* Remove Blank Space Automatically Before, After & middle of String */

function removeSpaces(string) {
 return string.split(' ').join('');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
<input placeholder="Enter Your Text" type="text" onkeypress="return AvoidSpace(event);" onblur="this.value=removeSpaces(this.value);">


</form>


Try This

<script language="JavaScript">
function RWS(str){
  return str.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ");
}
</script>

<form name=f1ex>
<textarea name="t2" rows="5" onChange="this.value=RWS(this.value)"></textarea>
</form>


you can use this jQuery method http://api.jquery.com/jQuery.trim/
val = $.trim(val); OR val = jQuery.trim(val);

0

精彩评论

暂无评论...
验证码 换一张
取 消