onChange or onBlur remove leading spaces

This should be an easy one and I've tried about a dozen different pieces of code to do this but nothing is working.

I would like to be able to remove leading spaces from required text boxes in my form before validating. onSubmit is where my validation is called.

Thanks!
allbird79Asked:
Who is Participating?
 
German_RummConnect With a Mentor Commented:
oops, my mistake:

onblur="this.value = this.value.replace(/^\s+/g, '')"
0
 
German_RummCommented:
Hi allbird79,

Try this:
    element.value = element.value.replace(/^\s+/g, '')

---
German Rumm.
0
 
allbird79Author Commented:
I added: onChange="element.value = element.value.replace(/^\s+/g, '');" to the end of the text box tag.

It didn't remove the space in the form.

Did I not put it in the code properly?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
German_RummCommented:
allbird79,

Better use onBlur(), there are some problems with onchange event handler when it changes contents of element it is attached to.

Or, you can do it in onSubmit() event handler, you are performing validation there anyway.
0
 
allbird79Author Commented:
I changed it to onBlur and it didn't work.

I've never figured out how to combine two events when using onSubmit. Currently my onSubmit is in my form tag and looks like this:
<form ... onSubmit="MM_validateForm('customerName','','R','companyName','','R','city','','R','province','','R','remarks','','R');return document.MM_returnValue">

How would I add the code you've provided?
0
 
German_RummCommented:
allbird79,

ok, you are using DW validation, don't want to mess with that. You textbox should look like this:

<input type="text" onblur="this.element = this.element.replace(/^\s+/g, '');" />
0
 
allbird79Author Commented:
I really don't want to mess with it :)

This is not working either...
The entire line is:

<input type="text" name="customerName" class="normal" size="40" onblur="this.value = this.value.replace(/^\s+/g, '')">

Do you see any errors?

0
 
German_RummCommented:
allbird79,

Strange.
http://www.car.ee/EE/Q_21411101.htm - tried that, everything worked in both IE and FF.
Do you want to remove leading spaces or trailing spaces?
0
 
allbird79Author Commented:
Sorry about that, I tested the wrong page that last time. It is working!!
Thanks,
Appreciate your help on this one.
0
 
German_RummCommented:
allbird79,

Thanks :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.