Correct input-Filter mode

Source: Internet
Author: User

When creating a form dynamically in builder mode, the user's input is arbitrary, obviously this is not allowed, we need to filter out the user unreasonable input and to prompt, Filter mode allows the developer to filter a set of objects through different criteria, and through the logical operation to connect them together.

According to the requirements of the form, the design input is an empty filter, a mailbox format filter, a length filter, and according to the user input results, its class diagram structure is as follows:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/8B/80/wKiom1hPqpHyRSkmAAAkJTHKwHg650.png-wh_500x0-wm_3 -wmp_4-s_4128288813.png "title=" filter mode. png "alt=" wkiom1hpqphyrskmaaakjthkwhg650.png-wh_50 "/>

Code implementation:

Var filter = class.extend ({controls:[],ctor:function (_controls = []) {This.controls  = _controls;},addcontrol:function (_control) {This.controls.push (_control);},filter:function () {return  [];}); Var emailfilter = filter.extend ({filter:function () {var myreg = /^ () ([A-zA-Z0-9]+[_ |\_|\.]?) *[a-za-z0-9][email protected] ([a-za-z0-9]+[_|\_|\.]?) *[a-za-z0-9]+\. [A-za-z] {2,3}$/;for (var i in this.controls) {Var txt = document.getelementbyid ( this.controls[i].id). Value;if (!myreg.test (TXT)) {Console.log (this.controls[i].label +  "does not conform to the message format");}}}); Var lengthfilter = filter.extend ({filter:function () {) {for (Var i in this.controls) { Var txt = document.getelementbyid (this.controls[i].id). Value;if (txt.length < 6) { Console.log (this.controls[i].label +  "length cannot be less than 6");}}}); Var emptyfilter = filter.extend ({filter:function () {) {for (VAR&NBSp;i in this.controls) {Var txt = document.getelementbyid (this.controls[i].id). value; if (txt ==  "") {Console.log (this.controls[i].label +  "cannot be Empty");}}});     <body>        <form id =   "Form1"  action =  "#"  method =  "POST" >         </form></body><script>var empty_filter,email_filter,length_filter; ( function () {var name = new input ("Mailbox", "username", "username", "text");var password  = new input ("Password", "password", "password", "password"); Var submit = new button ("Submit", "Sub", "Sub", SubmitForm), Var form  = new form ("Form1", []); Form.addcontrol (name). AddControl (password). AddControl (Submit). Build (); Empty_filter = new emptyfilter (); length_filter  = new lengthfilter (); email_filter&nbSp;= new emailfilter (); Empty_filter.addcontrol (name); Empty_filter.addcontrol (password); Email_ Filter.addcontrol (name); Length_filter.addcontrol (password);}) (); Function submitform () {empty_filter.filter (); Email_filter.filter (); Length_filter.filter ();} </script>


This article from "Go one stop two look back three" blog, please make sure to keep this source http://janwool.blog.51cto.com/5694960/1882313

Correct input-Filter mode

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.