validate text field so it does not have spaces

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.
Who is Participating?
ZvonkoConnect With a Mentor Systems architectCommented:
How about this:

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

bugs021997Connect With a Mentor Commented:
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;
Andrew MaurerConnect With a Mentor Commented:
I prefer to remove spaces server side.

<cfset NewString = Replace(theFormString, " ", "", "ALL")>
StingRaYConnect With a Mentor Commented:
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.
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.