Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'Html.Validate' and 'Html.ValidateFor'

Why do we use .Validate and .Validatefor in validation?

I am using that, but I am not getting any error message in the UI.

Code

<div>
    @{Html.BeginForm();}
    @Html.TextBoxFor(x => x.LastName, new { id = "txtLastName" })
    @{Html.Validate("LastName");}
    @{Html.ValidateFor(x=>x.LastName);}
    <input type="submit" id="btnSubmit" value="Submit" />
    @{Html.EndForm();}
</div>
like image 410
user2688364 Avatar asked Aug 16 '13 08:08

user2688364


People also ask

What does HTML Validator do?

An HTML validator is a quality assurance program used to check Hypertext Markup Language ( HTML ) markup elements for syntax errors. A validator can be a useful tool for an HTML user who receives data electronically from a variety of input sources.

What is W3C HTML Validator?

W3C validation is the process of checking a website's code to determine if it follows the formatting standards. If you fail to validate your website's pages based on W3C standards, your website will most likely suffer from errors or poor traffic owing to poor formatting and readability.

Can HTML be used for validation?

Conclusion- HTML Form ValidationMainly there are two ways to perform HTML form validation. The first is using the built-in functionality provided in HTML5, and the second is by using JavaScript. Using the first method, we don't need to write extra code.


1 Answers

This behavior is intentional. Both these helpers just register corresponding parameters for client-side validation, without actually showing any message should the validation fail. However this message can still be displayed in a ValidationSummary.

If you want to show the message specific to the field/parameter, you should use ValidationMessage or ValidationMessageFor instead:

@Html.ValidationMessage("LastName")
@Html.ValidationMessageFor(x=>x.LastName)
like image 200
Andrei Avatar answered Oct 22 '22 01:10

Andrei