[Angularjs] Form validation

Source: Internet
Author: User

Write in front

Submitting forms in development and validating the values of forms is a very common operation, and ANGULARJS provides very good support for form validation.

Demo

Form

<formname= "MyForm"novalidate Action=".. /api/order/lists/add "Method= "POST">    <P>        <inputtype= "text"name= "Name"Ng-model= "Order.name"value=""Required/>        <spanstyle= "Color:red"Ng-show= "Myform.name. $dirty && myform.name $invalid">            <spanNg-show= "Myform.name. $error. Required">The order name is required.</span>        </span>    </P>    <P>        <inputtype= "text"name= "Price"Ng-model= "Order.price"value=""Required/>        <spanstyle= "Color:red"Ng-show= "Myform.price. $dirty && myform.price $invalid">            <spanNg-show= "Myform.price. $error. Required">The unit price is a must.</span>        </span>    </P>    <inputtype= "Submit"name= "Name"ng-disabled= "Myform.name. $dirty && myform.name $invalid | | Myform.price. $dirty && myform.price. $invalid " /></form>

Service side

[HttpPost] [Route ("Api/order/lists/add")]         Public AsyncTaskPost ([frombody] order order) {return awaitTask.Factory.StartNew (() ={httpresponsemessage response=Newhttpresponsemessage (httpstatuscode.accepted); Order. Id=Guid.NewGuid ();                Lstorders.add (order); Response=NewHttpresponsemessage (httpstatuscode.accepted) {Content=NewStringcontent (Jsonconvert.serializeobject (New{_code = $, _data =lstorders}))                }; returnresponse;        }); }

Test

Post-submission Data

Summarize

$dirty: The form is filled with records

$valid: Field contents are valid

$invalid: Field contents are illegal

$pristine: The form does not fill in records

[Angularjs] Form validation

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.