Form validation of empty strings

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Form validation of empty strings



How can I make the form first name input field not accept empty strings with space characters " "


" "


<form asp-action="SaveRegistration" autocomplete="off">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FirstName" class="control-label"></label>
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>



Model:


public class ContactInfo
{
[Required(ErrorMessage = "This field is required")]
[StringLength(50)]
[DisplayName("First name")]
public string FirstName { get; set; }
}



With [Required] attribute the user can still submit with a string with just space characters " "


[Required]


" "



I know it's a simple question, but I am novice in ASP.NET MVC





you know, you can use [StringLength] and.. what deters you from using @Html.TextBoxFor()?
– Bagus Tesa
30 mins ago




[StringLength]


@Html.TextBoxFor()





msdn.microsoft.com/en-us/library/… try this?
– Bola
29 mins ago





@BagusTesa I am already using StringLength. How is Html.TextBoxFor better than <input asp-for="FirstName" ?
– Don Box
8 mins ago


Html.TextBoxFor


<input asp-for="FirstName"





@Bola How does MinLength helps? I want to prevent user from entering string of just space characters, I don't want to restrict to a minimum length
– Don Box
4 mins ago


MinLength





[Required(AllowEmptyStrings = false)] - is this work? Sounds like you can write custom RequiredAttribute with IsNullOrWhiteSpace check.
– Tetsuya Yamamoto
4 mins ago


[Required(AllowEmptyStrings = false)]


RequiredAttribute


IsNullOrWhiteSpace









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived