0

I am trying to remove spaces in value of a text filed using JS but its not working Here is what i have tried

HTML

<input name="lmt_c13" id="lmt_c13" onblur="verifyControlValue(this);" type="number" />

JavaScript

function verifyControlValue(control) {
    control.value = control.value.replace(/\s+/g, '');
    // other functionality based on control value
}

but this removes the whole value I am calling this function on onblur() event

Here is the JsFiddle http://jsfiddle.net/4gLb5k4L/8/

3
  • 2
    can you add a fiddle? Commented Aug 20, 2014 at 6:39
  • 2
    .replace(/\s+/g, ''); will remove spaces so if that isn't working, then something else is wrong with your code that you aren't showing us. Commented Aug 20, 2014 at 6:42
  • I updated your Fiddle to add the code in the correct place. This isn't a text field though, it's a number field. Commented Aug 20, 2014 at 6:51

2 Answers 2

1

See updated fiddle - http://jsfiddle.net/4gLb5k4L/6/

Why are you using type="number" for text? That's your problem.

HTML

<input name="lmt_c13" id="lmt_c13" type="text"></input>

JavaScript

var input = document.getElementById('lmt_c13');
input.addEventListener('blur', function () {
    this.value = this.value.replace(/\s+/g, '');
}, false);
Sign up to request clarification or add additional context in comments.

Comments

0

You are using a number field. value will only represent a correct value when a number is entered. Otherwise it will return an empty string.

So really you are running this code:

"".replace(/\s+/g, '');

Which is why you're getting a blank string back. Either use a text field or else let the number field do the validation for you.

2 Comments

actually the thing is that I am loading this page in Android webView and on a specific device Nexus 10 with android 4.4.2 this validation is not working
@AdilWaqar Then you should use parseInt/parseFloat instead and do your own validation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.