Model validation not working with RegularExpression Attribute RRS feed

  • Question

  • User-1506157797 posted


    I am doing EmployeeIdent field validation to accept only ALPHA NUMERIC data using Regular expression in MVC application created using scaffold template. Validation is working fine in "Edit" View. But, it is throwing error "TypeError: $.validator.methods[method] is undefined" in "Create" view while checking for regexression validation. Below are the details,

    public class Employee
            [Remote("VerifiyUserExists", "Employees", AdditionalFields = "EmployeeID")]
            [RegularExpression(@"^([a-zA-Z0-9]+)$", ErrorMessage = "Allowed only Alphanumeric value.")]
            [DisplayName("Employee #")]
            public String EmployeeIdent { get; set; }

    Tried to debug using Firbug and observed the following data annotations attributes execution sequence for Create view after entering value in textbox and pressing TAB key,

    1. Required

    2. Regex ( Type Error exception thrown while verifying this as mentioned above and skipped "Remote" attribute validation).

    For Edit View, all validation attributes are executed without any error in the sequence: Required, regex and Remote.

    I am not able to find where the actual problem is. Please help me out to resolve this.

    - Thanks


    Wednesday, January 9, 2013 2:07 AM


  • User1613327603 posted

    Please try with below code snippet.

     [RegularExpression(@"[A-Za-z0-9]*", ErrorMessage = "Please enter valid value.")]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 9, 2013 5:34 AM