Verify whether the string you entered is a date or a date
In angularjs, you want to verify whether the string you entered is a date time in the text box.
At the beginning, Insus. NET thought of regular expressions. This only verifies whether the date and time formats are correct. However, Insus. NET is powerless for the last day, 30, or 31, January 1, February.
Therefore, Insus. NET wants to use angularjs custom commands to verify and solve this problem.
In the ASP. net mvc project, create a controller and create an Action:
Controller source code:
Using System; using System. collections. generic; using System. globalization; using System. linq; using System. web; using System. web. mvc; namespace Insus. NET. controllers {public class CommonsController: Controller {public JsonResult ValidateDate (string date) {object _ Data; DateTime dt; if (DateTime. tryParse (date, out dt) {_ Data = new {result = true} ;}else {_ Data = new {result = false };} return new JsonResult {Data = _ Data, ContentEncoding = System. text. encoding. UTF8, JsonRequestBehavior = JsonRequestBehavior. allowGet };}}}Source Code
Next, you can write direve ve, which is a js file:
Angularjs code of validateDate:
AirExpressApp. directive ('validatedate', function ($ http, $ q) {return {restrict: 'AE', require: 'ngmodel', link: function ($ scope, element, attributes, ngModelController) {ngModelController. $ asyncValidators. dataValid = function (modelValue, viewValue) {var deferred = $ q. defer (); var obj ={}; obj. date = modelValue; $ http ({method: 'post', url: '/Commons/validatedate', dataType: 'json', headers: {'content-type ': 'application/json; charset = UTF-8 '}, data: JSON. stringify (obj ),}). then (function (response) {if (ngModelController. $ isEmpty (modelValue) | response. data. result) {deferred. resolve ();} else {deferred. reject () ;}}); return deferred. promise ;};}}});Source Code
The angularjs attribute is applied to html input:
Demo: