validate form - trim fields

I have an onchange event that trims this paticular field.  I can add this to each tag or is there a way that I can have this automatically implemented across all html fields.

<tr>
<td align="right" nowrap class="faxtext">Address Description: <textarea name="CONTACT_ADDR_DESCR" id="CONTACT_ADDR_DESCR" onchange="trim('CONTACT_ADDR_DESCR', this.value)" cols="32" rows="6" >Location Specific - Directions</textarea></td>
</tr>
LVL 3
gazdzidAsked:
Who is Participating?
 
guru_samiCommented:
Try this jquery sample.
It will add change event for all 'textarea' controls on the page and trim.
If you are not familiar with jquery: www.jquery.com
http://api.jquery.com/element-selector/
http://api.jquery.com/jQuery.trim/
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="scripts/jquery-1.4.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('textarea').change(function () {
                alert($(this).val());
                alert(jQuery.trim($(this).val()));
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <textarea id="TextArea1" cols="20" rows="2"></textarea><br />
        <textarea id="TextArea2" cols="20" rows="2"></textarea><br />
        <textarea id="TextArea3" cols="20" rows="2"></textarea><br />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
hieloCommented:
try putting these in the head of your document:
0
 
hieloCommented:
sorry, it got cut off. This is what I had:
<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script type="text/javascript">
	$(function(){
		$(":input").bind('change',function(){
			$(this).val( $.trim($(this).val() ) );
		});
	});
	</script>

Open in new window

0
 
govindarajan78Commented:
i hope you do validation while submitting the form, so the trimming there (form onsubmit or button onclick)
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.