-1

I have Required attribute for some strings in my view model, but that doesn't catch if user types, say, (yes, whitespace) into the text area. I need to prevent user to submit whitespace-only strings.

I am trying to use RegularExpression attribute on my view model's properties to validate the string. I've seen some answers such as Filtering "whitespace-only" strings in JavaScript or How to validate whitespaces using jquery/ajax in an MVC view but they don't seem to work properly (yes, before you ask, I am omitting leading and trailing /s if I'm taking it from Javscript to C#). The examples at the answers to those questions usually validate only against whitespace, or no whitespace at all. What I need is that I need at least some text to validate. Having some whitespace is no problem as long as I also have some non-whitespace string too.

What regex should I use (I am not good with regexes) in my attribute to validate some text that contains at least some non-whitespace text?

3
  • 5 seconds worth of googling has several answers, including stackoverflow.com/a/2788151/3279876 Commented Feb 15, 2016 at 14:35
  • @Sami well, I did google for several minutes before posting. if I did find a working solution, why would I post? Commented Feb 15, 2016 at 14:36
  • did you determine a solution for this? Commented Jul 8, 2020 at 19:05

4 Answers 4

3

The Regex needed is ^(.*\S.*)$. This will allow spaces if there is another "real" character.

In an attribute it would look something like.

//using System.ComponentModel.DataAnnotations;

[Required, RegularExpression(@"^(.*\S.*)$")]
public string Name { get; set; }

I tried adding Required(AllowEmptyStrings = false) first with no success, but that is what Microsoft says to do. It didn't work for me but the Regex above did.

Sign up to request clarification or add additional context in comments.

Comments

-1

You can add these data-annotations to your strings:

    [Display(Name = "Email")]
    [Required(ErrorMessage = "Please enter email address !", AllowEmptyStrings = false)]
    public string Email { get; set; }

The part AllowEmptyStrings = false will prevent you from saving empty strings (like a whitespace)

2 Comments

it doesn't work. added it, typed some space, and it validated, where it shouldn't.
Then [RegularExpression(@"^[^\s\,]+$", ErrorMessage = "Email Can't Have Spaces")]
-1

the regular expression ^\s+$ matches strings that consist only of whitespace.

the regular expression \S matches strings that contain some non-whitespace.

2 Comments

\S, when used with the RegularExpression attribute, won't allow a string with whitespace, even if it has non-whitespace characters.
You asked for a regular expression that matches "whitespace only". Duh.
-1

Try this expression: ^\S+$

Tested at http://regexr.com/3cq34.

1 Comment

Have you even read the question? Your own example doesn't work if the text contains any space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.