Skip to main content
Inspiring
November 29, 2012
Question

Control scanned field contents

  • November 29, 2012
  • 2 replies
  • 793 views

I have a cfform form for a blood product unit number.  Staff usually scan the bar code into this field, but I want some of the content to be stripped.

Embedded in the bar code are start and stop characters.  The start is always =.  Then the next 13 characters are the only ones I want, there is also a chack digit that is embedded at the end, but again, I only want the 13 characters after the =.

Also I want to make sure that what is submitted is exactly 13 characters and the first submitted character will always be a W.

Is this possible?

Thank you

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    November 30, 2012

    Your description confused me.

    ctreeves wrote:

    ...The start is always =.  Then the next 13 characters are the only ones I want, there is also a chack digit that is embedded at the end...

    That says to me that the form field contains a total of 15 characters. Is that so?

    The start is always =.  Then the next 13 characters are the only ones I want, there is also a chack digit that is embedded at the end, but again, I only want the 13 characters after the =. Also I want to make sure that what is submitted is exactly 13 characters and the first submitted character will always be a W.

    What you've described is validation. However, it remains unclear whether you want this validation to occur before or after the form has been submitted.

    If I understand it correctly, the original code has the following 15-character structure: =[13 characters, ignoring the square brackets]$. Here I have denoted the check digit at the end by $. Do you want all 15 characters to go into the form field and for validation (extraction of the 13 suitable characters) to occur after the form is submitted? Or else, do you want the 13 suitable characters to be extracted and put in the form field before it is submitted?

    ctreevesAuthor
    Inspiring
    November 30, 2012

    Sorry for not being more clear.

    I guess it doesn't matter where the validation takes place  - displayed in the form field for the user, or on submission.

    Currently the field is just a dumb text field with no restrictions.  The user use to enter the unit number by hand, but they now scan them - which saves them time.

    What they look like scanned is something like =W12021240156800, I only what what is in bold, I don't want the underlined text to end up in the database.

    The data I want will always start with a W followed by 12 numbers.  If the W could be forced to be a CAP that would be a bonus.

    Also, some users may still enter it by typing if they don't have a scanner available, so presumable they'd enter it correctly and with the =.

    Sorry to not reply sooner, I didn't get an e-mail notice, I'll check my settings.

    Thanks,

    Cliff

    BKBK
    Community Expert
    Community Expert
    December 1, 2012

    ctreeves wrote:

    I guess it doesn't matter where the validation takes place  - displayed in the form field for the user, or on submission.

    I think it does matter. Ideally, as a general rule, validation is done twice. First, on the value going in as form input, and then again on the value that comes out at the other end after the form has been submitted. The main advantage of validating before form-submission is that you avoid a round-trip to and from the server, when there is a trivial, easily corrected mistake in the input field. An added bonus is, this avoids upset in some clients.

    Currently the field is just a dumb text field with no restrictions.  The user use to enter the unit number by hand, but they now scan them - which saves them time.

    What they look like scanned is something like =W12021240156800, I only what what is in bold, I don't want the underlined text to end up in the database.

    The data I want will always start with a W followed by 12 numbers.  If the W could be forced to be a CAP that would be a bonus.

    Also, some users may still enter it by typing if they don't have a scanner available, so presumable they'd enter it correctly and with the =.

    This is a perfect use-case for <cfinput type="text">, should you wish to enforce strict validation on the client-side. Here is a test example:

    cfif isDefined("form.code")>

    <p><cfdump var="#form#"></p>

    </cfif>

    <cfform>

    <!--- I have assumed the code must be composed of a W followed by 12 digits. If it is not, modify the reguler expression pattern accordingly. --->

    <cfinput type="text" name="code" validate="regex" pattern="^W\d{12}\b$" message="The code must be a W followed 12 digits.">

    <cfinput type="hidden" name="code_required" value="You must enter the code.">

    <cfinput type="Submit" name="sbmt" value="Submit">

    </cfform>

    However, this validation might be too strict, hence inconvenient, for the user who scans his code in. If so, then leave out the attributes validate, pattern and message.

    Now, on to server-side validation. This validation is required.

    Example of server-side validation code (in the form's action page):

    <cfif isDefined("form.code")>

        <!--- I have assumed the code must be composed of a W followed by 12 digits. If it is not, modify the reguler expression accordingly. --->

        <cfset RE_match_array = REMatch("^W\d{12}\b$",form.code)>

        <cfif arrayLen(RE_match_array) GT 0>

        <!--- The code is valid --->

        <!--- In here goes business code which, amongst others, stores the form data to the database --->

        <cfelse>

        <!--- The code is not valid --->

        <!--- In here goes business code which, amongst others, redirects the user back to the form and informs him the code he entered was not valid --->

        </cfif>

    </cfif>

    Miguel-F
    Inspiring
    November 29, 2012

    Did you resolve this already?

    Does your form field begin with the start character '=' or is it embedded with some other random text?

    I might be over-simplifying this but you could do something like:

    if the form.field = '=W0123456789ABx'

    <cfset strippedvalue = Mid(form.field,2,13) />

    if the form.field = 'some_other_random_characters=W0123456789ABx_more_random_characters'

    <cfset strippedvalue = Mid(form.field,Find("=",form.field),13) /> <!--- this assumes the "=" is unique in the string, or at least the first occurrance is the one you want --->

    You are telling the Mid() function to only extract 13 characters so you know the length (unless the form.field has fewer characters than that after the "=")

    Does that help?