Skip to main content
Inspiring
July 28, 2015
Question

trim function

  • July 28, 2015
  • 1 reply
  • 766 views

Hi. I would like to use the Trim Function in a text box on my Add page when I insert something into our database. I just want to make sure that when users type a part number into this text box, they don't accidentally type in a space before or after the part number. Here's what my text box looks like:

<input type="text" name="Part_Number" id="Part_Number" maxlength="25" size="27" disabled="disabled">

I know how to use the Trim Function on an edit page, but not on an add page. Here's how I use it on an Edit page:

<input type="text" name="Part_Number" value="#Trim(Part_Number)#" maxlength="25" size="27">

This works because I have a value in the Part_Number field already, but how do I use this when I don't have a value entered into the database yet? Thanks for your help.

Andy

This topic has been closed for replies.

1 reply

WolfShade
Legend
July 28, 2015

You put the trim() as the insert value in your query, not in the form input.

<cfquery name="anon" datasource="#application.dsn#">

INSERT into tableA (col1, col2, part_number)

VALUES (<cfqueryparam value="#trim(form.input1)#" />,<cfqueryparam value="#trim(form.input2)#" />,<cfqueryparam value="#trim(form.Part_Number)#" />)

</cfquery>

HTH,

^_^

Inspiring
July 28, 2015

WolfShade,

    Thank you for the reply. That works good, but I need to catch the space before it goes to the Insert page because we don't want the same part number to be entered twice if it's in an open order. Do I need to use some javascript to catch these spaces? If so, how do I do that? Maybe this would be the best thing to do. If there are any spaces before or after the part number, have a javascript alert box say something about spaces are not allowed before or after a part number. Do you know Javascript at all?

Andy

Inspiring
July 29, 2015

You can check it on the form submission and the insert into the database.

When you submit the form, run some javascript that will be trim the text in the inputs and then post the form. http://stackoverflow.com/questions/8120983/trim-spaces-from-form-input

You can then also trim it when you add it to the database. When you do the insert using coldfusion, just use the trim function then as well.