Reputation: 137
I want to display different error messages for a text box, I am using view only and I don't want to use a model for validation:
@Html.TextBox("txtFirst", "", htmlAttributes: new { @class = "required email" })
This field is required and it must be valid email. The above solved my problem but I am not able to display different validation message. If a user does not enter a value then error message is please enter email address. If invalid email address then it should display please enter valid email. Please keep in mind i don't want to use model for validation. I want to use view only.
Upvotes: 1
Views: 1639
Reputation: 6607
Basically, by circumventing the MVC pattern you can't get what you are looking for without using javascript. This would work.
Put this next or under your input to show an error
@Html.Label("lblError", "")
<input type="submit" id="submitButton" value="Save Data" />
//Search By Entity Info
submitButton.onclick = function (event) {
if(isNotEmpty(firstName)) {}; // for multiple fields, put them all into a separate call and return true only if they all are OK
else {
return false;
}
}
//Validation Methods
function isNotEmpty(field) {
var fieldData = field.value;
if (fieldData == null || fieldData.length == 0 || fieldData == "") {
field.className = "FieldError"; // this class "FieldError" would need to use styling CSS to give the field the look that it was in error
var errorMessage = document.getElementById("lblError");
errorMessage.Value = "Error - Your custom text here";
return false;
}
}
This is just an example and would need tweaked
But I would advise strongly FOR using ViewModels. You might as well be using straight HTML rather than the MVC 3 framework. You're doing yourself a disservice by not using MVC the way it is meant to be used.
Look at the amount of code I had to write! And ViewModels provide client side as well as server side validation. So you don't get burned just because you only relied on client side and someone either sent invalid data thru or went around it by posting data directly thru the URL and pushing bad data into your DB
Upvotes: 1
Reputation: 8393
Why would you not want to use data annotations?
Relying only upon client side validation is not a good idea as a user could have javascript disabled within their browser, be using an older browser in the case of HTML5 form validation. This only leads to a user submitting invalid form data. You should always use a combination of client / server side validation.
I would strongly recommend you create a view model and enable data annotations. For example note, the usage of the required attribute and a custom email attribute for your validation purposes:
[Required(ErrorMessage = "Please enter email address"]
[Email(ErrorMessage = "Please enter valid email")]
public string Email { get; set; }
An example email validation attribute:
public class EmailAttribute : RegularExpressionAttribute
{
private const string EmailReg = @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";
public EmailAttribute() : base(EmailReg)
{
}
}
Now, as per your question if you want to use only client side validation then you should definitely not rely upon HTML5 form validation. A quick way of enabling validation would be to get the jquery validation plugin.
Demo here: http://jquery.bassistance.de/validate/demo/
Upvotes: 0