0

I would like to implement Data Validations on my ASP MVC app. I am currently using Data Annotations like this:

[Required]
public string UserName { get; set; }

Which would then turn into something like

<input type='text' ... data-required>

I can validate it fine using jquery unobtrusive validation, however, this project does not have jQuery. It is built straight from Javascript and we plan to keep it that way.

Is there any way I can do this without jQuery?

7
  • [Required] means it will be validated by the MVC framework when the form is posted. If you want client side validation I guess you'll have to write it in javascript. Commented Jun 29, 2016 at 9:34
  • 1
    Data validation can be implemented in JS with something like egkyron. With it you define your validation rules for the model, not the UI, just as in the server-side. You would have to implement a binding between the validation result produced by egkyron and the UI to display the messages. The src/examples may help you (they are for Angular and React though). I could create a fiddle, if you could provide some more information, e.g. what client libraries are you using/are you supposed/allowed to use? Commented Jun 29, 2016 at 10:16
  • what use instead of jquery.type script ,java script or etc ? Commented Jun 29, 2016 at 10:18
  • We're only using Javascript. I'll take a look at egkyron. Thanks. Commented Jun 29, 2016 at 19:08
  • I've created a demo plunk using plain JS. It has no UI integration - but I have added some pointers in script.js. Hope it helps in some way. Commented Jun 30, 2016 at 8:09

2 Answers 2

1

So, as per the comment, there are libraries that implement model validation in Javascript. I have written one, Egkyron, and being using it in my work. With these libraries you define your validation rules for the model, not the UI, just as in the server-side.

Assume a User model defined in JS as:

function User() {
  this.userName = null;
  this.password = null;
  this.passwordVerification = null;
}

You can define its validation rules as the equivalent of JS annotations:

User.validators = {
  // key is property name from the object; value is the validation rules
  userName: [
    // the userName is required...
    'required',
    // ...and some arbitrary rules for demonstrating:
    // "the user name starts with a lower-case letter and consists only of lower-case letters and numbers"
    ['regexp', {re: /^[a-z][a-z0-9]*$/}],
    // "the length of the user name is between 5 and 8 characters (inclusive)"
    ['length', {min: 5, max: 8}]
  ]
};

If using Babel or Typescript, you can check out the decorators, a proposal for the ES7 specification. A TS class could be annotated as:

class User {
    @Required()
    @RegExp(/^[a-z][a-z0-9]*$/)
    @Length({min: 5, max: 8})
    userName: string;
    ...
}

This is very close to what you write in the server side with Java or C#. In fact, in a previous project, we generated the JS classes + validation rules from the server-side C# classes.

Then, assuming you get hold of a User object, you can validate it with Egkyron as:

var validator = new egkyron.Validator(/* see the example for constructor arguments */);
var user = ...; // instance of a User
var validationResult = validator.validate(user).result;

The validator is reusable; if user = new User() the validationResult looks like:

{   // validation result for the given User
    _thisValid: true, // no validation rules of **this** object failed
    _validity: null,  // there are no validation rules for this object (only for its properties)
    _childrenValid: false, // its properties and/or children objects are invalid
    _children: {      // detailed report of its children objects/properties
        userName: {   // child property name
            _thisValid: false, // the userName is invalid (required but not given)
            _children: null,   // terminal node, no children
            _validity: { // detailed report about each validation rule
                required: { isValid: false, ... }, // the "required" constraint failed
                regexp: { isValid: true, ... },    // empty value => regexp validity defaults to true
                length: { isValid: true, ... }     // empty value => length validity defaults to true
            }
        },
        ...
    }
}

Having acquired a validation result, you probably want to present it to the UI. There are myriads of different requirements and tiny variations upon them. I believe it is impossible to satisfy all of them. Even if we could satisfy them all, the library size would be enormous and, most probably, the library itself not really usable.

So Egkyron leaves the UI integration to the user. There are examples, and I will happily answer any questions/issues.

Besides the examples, here is a plunk with the plain browser JS example above.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want client side validation you have to write your own library for data-**** tags to validate input or consider to add JQuery Unobtrusive Validation.

4 Comments

what use instead of jquery.type script ,java script or etc ?
@MohsenZahedi why ?
so , for display need to a language UI for example jquery javascript type script or etc
@MohsenZahedi Sorry but, I do not understand what you are talking about

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.