validate text field so it does not have spaces

Posted on 2007-08-05
Last Modified: 2013-11-05
I have a text field in a form. The text field is required.  It has to have seven characters.  I want to validate the field to check that there are no spaces.  by the way, I'm using coldfusion, and javascript to validate the form.
Question by:tiredntroubled
    LVL 14

    Assisted Solution

    function hasWhiteSpace(s)
         reWhiteSpace = new RegExp(/^\s+$/);
         // Check for white space
         if (reWhiteSpace.test(s)) {
              alert("Please Check Your Fields For Spaces");
              return false;
    return true;
    LVL 9

    Assisted Solution

    by:Andrew Maurer
    I prefer to remove spaces server side.

    <cfset NewString = Replace(theFormString, " ", "", "ALL")>
    LVL 15

    Assisted Solution

    try this

    function checkSpaces(str)
        wsPat = /\s+/g;
        return wsPat.test(str);

    this will return true when at least one space found in the given string, otherwise return false.
    LVL 63

    Accepted Solution

    How about this:

    <cfinput type="text" name="yourFname" validate="regular_expression" pattern="^\\w{7}$" required="yes" message="Enter seven characters.">


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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.

    This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
    Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    761 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

    11 Experts available now in Live!

    Get 1:1 Help Now