• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1403
  • Last Modified:

validate text field so it does not have spaces

Hi,
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.
0
tiredntroubled
Asked:
tiredntroubled
4 Solutions
 
bugs021997Commented:
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;
}
0
 
Andrew MaurerCommented:
I prefer to remove spaces server side.

<cfset NewString = Replace(theFormString, " ", "", "ALL")>
0
 
StingRaYCommented:
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.
0
 
ZvonkoSystems architectCommented:
How about this:

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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now