Support for ASP. net mvc and WebFroM Form Verification Framework ValidationSuar,

Source: Internet
Author: User

Support for ASP. net mvc and WebFroM Form Verification Framework ValidationSuar,

1. Support for dual verification between the javascript end and the backend (the front end currently depends on jquery. validate. js and can also be expanded by itself)

2. concise code

3. Convenient calls

4. Complete Functions

Usage:

Create an initialization class and initialize all the items to be verified in the class. The syntax is concise and can be managed in a unified manner. After writing this class, your verification is completed by 70%.

Function introduction:

Default Add type (email, mobile phone, qq, etc)

AddRegex Regular Expression verification is used when Add cannot meet the requirements

AddFunc uses js functions for verification. This function is generally used for validation of business logic. It has powerful functions and can satisfy various verifications. (Note: After addFunc function verification, the backend needs to be re-verified, so we can use the above two methods for verification, and try to use the above)

Using System; using System. collections. generic; using System. linq; using System. web; using SyntacticSugar; namespace ValidationSuarMVC. models {public class Validates {public static void Init () {// login ValidationSugar. init (PageKeys. LOGIN_KEY, ValidationSugar. createOptionItem (). set ("userName", true/* required? */, "userName "). addRegex ("[a-z, A-Z]. * "," the user name must start with a letter "). addRegex (". {5, 15} "," the length is 5-15 characters "). addFunc ("checkUse RName "," the user name does not exist. Enter admin1 to try "). toOptionItem (), ValidationSugar. createOptionItem (). set ("password", true, "password "). addRegex ("[0-9]. * "," the user name must start with a number "). addRegex (". {5, 15} "," the length is 5-15 characters "). toOptionItem (); // register ValidationSugar. init (PageKeys. REGISTER_KEY, ValidationSugar. createOptionItem (). set ("userName", true, "userName "). addRegex ("[a-z, A-Z]. * "," the user name must start with a letter "). addRegex (". {5, 15} "," the length is 5-15 characters "). addFunc ("checkU SerName "," the user name already exists! "). ToOptionItem (), ValidationSugar. createOptionItem (). set ("password", true, "password "). addRegex (". {5, 15} "," the length is 5-15 characters "). toOptionItem (), ValidationSugar. createOptionItem (). set ("password2", true, "password "). addRegex (". {5, 15} "," the length is 5-15 characters "). addFunc ("confirmPassword", "inconsistent passwords "). toOptionItem (), ValidationSugar. createOptionItem (). set ("sex", true, "gender "). addRegex ("0 | 1", "incorrect value "). toOptionItem (), ValidationSugar. createOptionItem (). set ("email", true, "email "). add (ValidationSugar. optionItemType. mail, "Incorrect email format "). toOptionItem (), ValidationSugar. createOptionItem (). set ("mobile", false, "mobile "). add (ValidationSugar. optionItemType. mobile, "Incorrect Mobile phone format "). toOptionItem (), ValidationSugar. createOptionItem (). set ("qq", false, "qq "). addRegex (@ "\ d {4, 15}", "Incorrect QQ number format "). toOptionItem (), ValidationSugar. createOptionItem (). set ("education", true, "education", true/* checkbox multiple choice mode */). addRegex (@ "\ d {}", "incorrect value "). toOptionItem ());}}}

We can use Global. cs registration.

Verification is generally divided into two types

1. submit submission

Get the binding information and hand it to viewbag.

PostRegister is also a line to complete background Verification

View

1. Reference js and write the initialization Function

2. Place @ Html. Raw (ViewBag. validationBind) at the bottom of the page

VIEW complete code:

@ {ViewBag. Title = "Register"; Layout = null ;}< html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <meta http-equiv =" Content-Type "content =" text/html; charset = UTF-8 "/> <script src ="/Content/jquery-validation-1.13.1/lib/jquery-1.9.1.js "type =" text/javascript "> </script> <script src ="/Content/ the jquery-validation-1.13.1/dist/jquery. validate. js "type =" text/javascript "> </script> <script src ="/Content/validation. sugar. js "type =" text/javascript "> </script> <script src ="/Content/jquery-validation-1.13.1/lib/jquery. form. js "type =" text/javascript "> </script> <link href ="/Content/jquery-validation-1.13.1/validation.sugar.css "rel =" stylesheet "type =" text/css "/> <script type = "text/javascript"> $ (function () {var factory = new validateFactory ($ ("form"), " "); factory. init () ;}); // check whether the user name already exists function checkUserName () {// change it to ajax async: false var userName = $ ("[name = userName]"). val (); if (userName = "admin1" | userName = "admin2") {return false;} return true;} // verify that the password is consistent function confirmPassword () {return $ ("[name = password]"). val () = $ ("[name = password2]"). val () ;}</script> <style> td {height: 30px; padding: 5px ;}</style> 

Just a few lines of code to complete a registration

The effect is as follows:

It is good to support css. Self-beautification

2. ajax writing

Change the submit parameter to a button and write an event.

Download DEMO:

Http://xiazai.jb51.net/201506/other/sunkaixuan-ValidationSuarMVC-master.zip

Related Article

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.