Reputation: 3050
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/
Upvotes: 0
Views: 69
Reputation: 78525
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.
Upvotes: 0
Reputation: 2986
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);
Upvotes: 1