Extend jQuery. validate in an instance that verifies form

Source: Internet
Author: User

 

Main keywords Case 1: Hangzhou, Zhejiang, Wenzhou

Main keyword Scenario 2: Zhejiang | Jiangsu | Shanghai, but spaces and | cannot be used in combination, that is, situations 1 and 2 cannot be mixed.

"Or" keywords: Education | Middle School | Primary School

Keywords: Principal-Professor-University

In addition, the number of primary keywords + or keywords cannot exceed 10

Unlimited number of non-keywords

Original article, reproduced Please note: http://www.cnblogs.com/phpgcs/

2 // main keyword validate 1 // user can input words with backspace or | splited 0 $. validator. addMethod ("kw_main_validate", function (value, element) {1 var kwmain =/^ ([\ u4e00-\ u9fa5a-zA-Z0-9] {} \ s ?) {} $ | ^ ([\ U4e00-\ u9fa5a-zA-Z0-9] {} \ | ?) {1, 10} $/; 2 // full match, use ^ and $3 return kwmain. test (value); 4}) 5 // or keyword validate 6 // user can input words with | splited 7 $. validator. addMethod ("kw_or_validate", function (value, element) {8 var kwmain =/^ ([\ u4e00-\ u9fa5a-zA-Z0-9] {} \ | ?) {1, 10} $/; 9 return kwmain. test (value); 10}) 11 // not keyword validate 12 // user can input words with-splited 13 $. validator. addMethod ("kw_not_validate", function (value, element) {14 var kwmain =/^ ([\ u4e00-\ u9fa5a-zA-Z0-9 }\-?) {1, 10} $/; 15 return kwmain. test (value); 16}) 17 // main kw + or kw <= 10 18 $. validator. addMethod ("checkMax10", function (value, element) {19 var main_string = $ ('# keyword_main '). val (); 20 var or_string = $ ('# keyword_or '). val (); 21 var main_num = main_string.split (/[\ s \ |]/). length; 22 var or_num = or_string.split (/[\ s \ |]/). length; 23 return (main_num + or_num-10)> 0 )? False: true; 24}) 1 rules: {5 keyword_main: {6 required: true, 7 kw_main_validate: true, 8 checkMax10: true 9}, 13 keyword_or: {14 kw_or_validate: true, 15 checkMax10: true 16}, 17 keyword_not: {18 kw_not_validate: true 19}, 20}, 21 messages: {14 keyword_main: {13 required: "Enter keywords ", 12 kw_main_validate: "Please check that the entered keywords are separated by spaces or" | "and cannot be mixed.", 11 checkMax10: "the total number of primary keywords and or keywords cannot exceed 10" 10 }, 9 keyword_or: {8 kw_or_validate: "The input keywords are separated by" | ". 7 checkMax10:" the total number of primary keywords and or keywords cannot exceed 10 "6}, 5 keyword_not: {4 kw_not_validate: "input keywords are separated by"-"." 3}, 2 },

^ Abc $

An online Regular Expression tool: http://tool.chinaz.com/regex,full matching is required.

Reference: http://topmanopensource.iteye.com/blog/568593

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.