2

i have a property in my EmployeeViewModel.cs as below

[Required]
[RegularExpeession]
public string EmpName {get;set;}

i am maintaining regex for not to allow special characters in my empName

when i give whitespace(press space button 2 times) in my text box the RequiredField validation is not firing because it is considering whitespace as a value

how to check this whitespace issue using required attribue.

could some one help me

SAREKA AVINASH
  • 437
  • 8
  • 15
  • You can try settings this property of the `Required` attribute `AllowEmptyStrings` to `false`. More info here https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.requiredattribute.allowemptystrings?view=netcore-3.1 – E. Shcherbo Oct 17 '20 at 18:17
  • AllowEmptyStrimg is not working here because whitespace is considering as a value – SAREKA AVINASH Oct 18 '20 at 01:20

2 Answers2

1

You can add another validation to your model called AllowEmptyStrings

[Required(AllowEmptyStrings = false, ErrorMessage = "Please Provide Emp Name")]
[RegularExpeession]
public string EmpName {get;set;}
0

This operation cannot be performed using: Required. I think you should use Regular Expression:

\S 

which equals do a search for non-whitespace characters in a string.

Useful post and answer by: engvard

What is the Regular Expression For “Not Whitespace and Not a hyphen”

Krzysztofz01
  • 432
  • 5
  • 13