Verify whether the string you entered is a date or a date

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.