Can anyone help me write a Reg Exp for CFINPUT Validation to prevent special char

Posted on 2009-04-30
Last Modified: 2012-05-06

I have a Cold Fusion form using <CFinput>  and I need to validate that the field is only:

letters - upper and lower case are OK
numbers are OK
hyphens are OK

but no special characters like  / \.,?!  spaces  etc

This field will become the name of a file.


<cfinput type="text" name="myfilename" message="  " pattern="  " validateat="onSubmit" validate="regular_expression" required="yes">

Open in new window

Question by:bigmikey88
    LVL 51

    Accepted Solution

    I about to run out the door, but you could try something like the attached.  

    However, you should also scrub the value on the server side as well.  So you are not depending on javascript only. I usually use a CF regex to replace all invalid characters with an underscore "_" (or something safe).
    	<cfinput type="text" 
    		name="myfilename" message="Invalid characters for filenames found" 
    		validate="regular_expression" required="yes">
    	<cfinput type="submit" name="submitButton">

    Open in new window

    LVL 51

    Expert Comment

    > I about to run out the door,

        I _am_ about to run out the door   (hence the bad spelling ;-)

    Author Closing Comment

    Excellent.  That worked perfectly.  Thank you very much.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
    Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now