Mazen Elkashef
Mazen Elkashef

Reputation: 3499

What's the best way to constraint validation to not allow any spaces in ASP.NET

I have different cases:

  1. No spaces allowed at all
  2. No spaces allowed at the beginning or the end of the string only

..A little question, is it good to check (to validate) the input for spaces through the RegEx of the RegularExpressionValidator ?

Upvotes: 1

Views: 6540

Answers (5)

Justin Morgan
Justin Morgan

Reputation: 30715

In your previous question, you mentioned you wanted from 0 to 50 characters. If that's still the case, here's what you want:

/^\S{0,50}$/
/^(?!\s).{0,50}(?<!\s)$/

As of right now, I think these are the only regexes posted that allow for less than one letter with the first pattern, and less than two letters with the second pattern.

Regexes are not a "bad" thing, they're just a specialized tool that isn't suited for every task. If you're trying to validate input in ASP.NET, I would definitely use a RegularExpressionValidator for this particular pattern, because otherwise you'll have to waste your time writing a CustomValidator for a pretty meager performance boost. See my answer to this other question for a little guidance on when and when not to use regex.

In this case, the reason I'd use a regex validator has less to do with the pattern itself and more to do with ASP.NET. A RegularExpressionValidator can just be dragged and dropped into your ASPX code, and all you'd have to write would be 10-21 characters of regex. With a CustomValidator, you'd have to write custom validation functions, both in the codebehind and the JavaScript. You might squeeze a little more performance out of it, but think about when validation comes into play: only once per postback. The performance difference is going to be less than a millisecond. It's simply not worth your time as a developer -- to you or your employer. Remember: Hardware is cheap, programmers are expensive, and premature optimization is the root of all evil.

Upvotes: 1

Quick Joe Smith
Quick Joe Smith

Reputation: 8222

The System.String class contains everything you need:

No spaces allowed at all

This will handle the case of spaces only:

bool valid = !str.Contains(" ");

If you need to check for tabs as well:

char[] naughty = " \t".ToCharArray();
bool fail = (str.IndexOfAny(naughty) == -1);

There are other whitespace characters you could check for, see Character Escapes for more details.

No spaces allowed at the beginning or the end of the string only

A bit simpler, since Trim() will remove any kind of whitespace, including newlines:

bool valid = str.Length == str.Trim().Length;

Upvotes: 0

Aliostad
Aliostad

Reputation: 81660

No spaces allowed at all:

^\S+$

No spaces allowed at the beginning or end:

^\S+.*\S+$

Upvotes: 1

Tejs
Tejs

Reputation: 41236

You know the saying about regex's (now you have two problems) - this is doable without regex and should be more performant and easier to read.

string checkString = /* whatever */
if(checkString.IndexOf(" ") > -1)
   // Failed Condition 1

if(checkString.Trim() != checkString)
   // Failed Condition 2

Upvotes: 1

BoltClock
BoltClock

Reputation: 723538

The \S escape sequence matches anything that isn't a whitespace character.

Thus the regexes that you need follow:

  1. ^\S+$

  2. ^\S.*\S$

Upvotes: 3

Related Questions