With HTML5 url input validation assume url starts with http://

The code for this should not interrupt the user’s action, but should instead wait until the user leaves the form field to check the input text for “http”. So use “onblur” instead of “onkeyup”. Then, just see if the string contains “http” using indexOf. If not, it will return -1, which is falsey. function checkURL … Read more

Model state validation in unit tests

TL;DR If you don’t want to read the entire article provided by Youssef and want a quick solution to how to make ModelState.IsValid return false. Do this. [TestMethod] public void TestLogin_InvalidModel() { AccountController controller = CreateAccountController(); // new code added –> controller.ModelState.AddModelError(“fakeError”, “fakeError”); // end of new code … var response = controller.PostLogin(new LoginModel() { … Read more

ViewModel validation for a List

If you are using Data Annotations to perform validation you might need a custom attribute: public class EnsureOneElementAttribute : ValidationAttribute { public override bool IsValid(object value) { var list = value as IList; if (list != null) { return list.Count > 0; } return false; } } and then: [EnsureOneElement(ErrorMessage = “At least a person … Read more

MaxLength Attribute not generating client-side validation attributes

Try using the [StringLength] attribute: [Required(ErrorMessage = “Name is required.”)] [StringLength(40, ErrorMessage = “Name cannot be longer than 40 characters.”)] public string Name { get; set; } That’s for validation purposes. If you want to set for example the maxlength attribute on the input you could write a custom data annotations metadata provider as shown … Read more

techhipbettruvabetnorabahisbahis forumueduseduedusedusedusedueduseduedusedu