c# - MVC - Client Validation -
i trying learn mvc , got problem regarding custom validation. wanted create validation these 2 properties
[required] [display(name = "country")] [range(1, int.maxvalue, errormessage = "country required")] public country country { get; set; } [required] [phone] [countryphonevalidation] public string phone { get; set; } phone's value should depending on country's value. country gets values enumeration , displays them in dropdown list. want each country have specific phone number prefix.
for example usa phone numbers should start 555.
my countryphonevalidation.cs validation this:
public class countryphonevalidation : validationattribute, iclientvalidatable { protected override validationresult isvalid(object value, validationcontext validationcontext) { bool validationflag = false; object instance = validationcontext.objectinstance; type type = instance.gettype(); propertyinfo propertyinfo = type.getproperty("country"); object propertyvalue = propertyinfo.getvalue(instance); //var propertyvalue = validationcontext.objecttype.getproperty("country").getvalue(validationcontext.objectinstance); if (propertyvalue == null) { validationflag = false; } else { switch ((int)propertyvalue) { case 1: if (value.tostring().startswith("210")) { validationflag = true; } break; case 2: if (value.tostring().startswith("555")) { validationflag = true; } break; } } if (!validationflag) { return new validationresult("that phone number not valid country (isvalid)"); } else { return null; } } public ienumerable<modelclientvalidationrule> getclientvalidationrules(modelmetadata metadata, controllercontext context) { string errormessage = "that phone number not valid country (getclientvalidationrules)"; modelclientvalidationrule countryphonerule = new modelclientvalidationrule(); countryphonerule.errormessage = errormessage; countryphonerule.validationtype = "countryphone"; yield return countryphonerule; } } and countryphonevalidation.js this:
jquery.validator.addmethod("countryphone", function (value, element, params) { var phonenumber = value; var country = parseint(document.getelementbyid('country').value); if (country == 1) { if (phonenumber.startswith("210")) return true; else return false; } else if (country == 2) { if (phonenumber.startswith("555")) return true; else return false; } }); jquery.validator.unobtrusive.adapters.add("countryphone", function (options) { options.rules["countryphone"] = {}; options.messages["countryphone"] = options.message; }); my custom client side validation works.
if pick usa , enter "1346579" phone number message "that phone number not valid country (getclientvalidationrules)".
the problem ignores phone attribute validation.
if give "lorem" phone number, instead of getting error message phone attribute, again "that phone number not valid country (getclientvalidationrules)" validator.
if pick usa , give "555lorem" number, don't error message custom validator (since don't check) passes phone attribute validation.
the phone validation works when hit submit. "the phone field not valid phone number." server validation.
any idea why happen , should do?
btw required attribute works fine.
aside that, started trying things out , did that:
var errortype = 0; jquery.validator.addmethod("countryphone", function (value, element, params) { var phonenumber = value; var country = parseint(document.getelementbyid('country').value); if (isnan(phonenumber)) { errortype = 1; return false; } if (country == 1) { if (phonenumber.startswith("210")) return true; else return false; } else if (country == 2) { if (phonenumber.startswith("555")) return true; else return false; } }); jquery.validator.unobtrusive.adapters.add("countryphone", function (options) { options.rules["countryphone"] = {}; if (errortype == 1) options.messages["countryphone"] = "error type 1"; else options.messages["countryphone"] = options.message; }); but totally clueless js , didn't work. tried assign 1 , return false every time (without isnan condition) again didn't work.
thanks
Comments
Post a Comment