Data validation and regular expression validation for Extjs forms

Source: Internet
Author: User

Data validation and regular expression validation for Extjs forms
Extjs Form Verification includes empty verification, simple verification in vtype format, advanced custom password verification, and regular expression verification. Verify that code can be written using the script provided by js, but ext encapsulates the form and allows the customer to expand it, therefore, the verification provided by Extjs can greatly simplify the verification judgment. Before verification, read the following two statements:

// Put Ext. QuickTips. init () in the function () {} of onReady; // provides the prompt information function for the component. The main prompt information of form is the error message verified by the client. Ext. form. Field. prototype. msgTarget = 'day'; // method of prompting. The enumerated value is:
Qtip-a prompt is displayed when you move the cursor over the control;
Title-the title of the browser is displayed, but the test result is the same as that of qtip;
Under-an error message is displayed under the control;
Side-an error icon is displayed on the right of the control. An error prompt is displayed when you point to the icon. default value;
Id-[element id] the error message is displayed in the HTML element of the specified id.

1. A simplest example: Empty Verification
The Code is as follows:
// Empty verification parameters allowBlank: false // false cannot be blank. The default value is true blktext: string // error message when null.

Js Code:
The Code is as follows:
Var form1 = new Ext. form. formPanel ({width: 350, renderTo: "form1", title: "FormPanel", defaults: {xtype: "textfield", inputType: "password"}, items: [{fieldLabel: "cannot be blank", allowBlank: false, // do not allow blank text: "cannot be blank", // error message, the default value is This field is required! Id: "blanktest",}]});

2. Simple verification in vtype format.
In this example of email verification, rewrite the items configuration of the code above:
The Code is as follows:
Items: [{fieldLabel: "cannot be blank", vtype: "email", // verify vtypeText: "invalid email address", // error message, by default, I will not mention id: "blanktest", anchor: "90% "}


You can modify the above vtype to the following extjs vtypes that support verification by default:
// The default supported vtype in form Verification

1. alpha // only letters can be entered, and other characters (such as numbers and special symbols) cannot be entered)
2. alphanum // only letters and numbers can be entered.
3. email // verification. The required format is ""
4. url // url format verification, required format is http://www.baidu.com

3. Advanced User-Defined password verification
The previous verification is provided by extjs, and we can also customize the verification function.
The Code is as follows:
// Use the Ext. apply method to add a custom password verification function (you can also use another name)
Ext. apply (Ext. form. VTypes, {password: function (val, field) {// val refers to the value of the text box here, and field refers to the text box component. You need to understand this meaning if (field. confirmTo) {// confirmTo is our custom configuration parameter. It is generally used to save the id value of another component var pwd = Ext. get (field. confirmTo); // get the value of the id of confirmTo return (val = pwd. getValue () ;}return true ;}});


// Configure items Parameters
Items: [{fieldLabel: "password", id: "pass1" ,},{ fieldLabel: "Confirm password", id: "pass2", vtype: "password ", // custom authentication type vtypeText: "The two passwords are inconsistent! ", ConfirmTo:" pass1 ", // id of another component to be compared}

4. Regular Expression Verification
The Code is as follows:
New Ext. form. textField ({fieldLabel: "name", name: "author_nam", regex:/[\ u4e00-\ u9fa5]/, // Regular Expression in /...... [\ u4e00-\ u9fa5]: only Chinese characters can be entered. regexText: "Chinese only! ", // Regular Expression error prompt allowBlank: false // This verification is still valid. Do not leave it blank.
 })

My projects:






Some custom verification functions
  1. Ext. apply (Ext. form. VTypes,
  2. {
  3. Daterange: function (val, field)
  4. {
  5. Var date = field. parseDate (val );
  6. // We need to force the picker to update values to recaluate the disabled dates display
  7. Var dispUpd = function (picker)
  8. {
  9. Var ad = picker. activeDate;
  10. Picker. activeDate = null;
  11. Picker. update (ad );
  12. };
  13. If (field. startDateField)
  14. {
  15. Var sd = Ext. getCmp (field. startDateField );
  16. Sd. maxValue = date;
  17. If (sd. menu & sd. menu. picker)
  18. {
  19. Sd. menu. picker. maxDate = date;
  20. DispUpd (sd. menu. picker );
  21. }
  22. }
  23. Else if (field. endDateField)
  24. {
  25. Var ed = Ext. getCmp (field. endDateField );
  26. Ed. minValue = date;
  27. If (ed. menu & ed. menu. picker)
  28. {
  29. Ed. menu. picker. minDate = date;
  30. DispUpd (ed. menu. picker );
  31. }
  32. }
  33. Return true;
  34. },
  35. Password: function (val, field)
  36. {
  37. If (field. initialPassField)
  38. {
  39. Var pwd = Ext. getCmp (field. initialPassField );
  40. Return (val = pwd. getValue ());
  41. }
  42. Return true;
  43. },
  44. PasswordText: 'The passwords entered twice are inconsistent! ',
  45. Chinese: function (val, field)
  46. {
  47. Var reg =/^ [/u4e00-/u9fa5] + $/I;
  48. If (! Reg. test (val ))
  49. {
  50. Return false;
  51. }
  52. Return true;
  53. },
  54. ChineseText: 'enter Chinese characters ',
  55. Age: function (val, field)
  56. {
  57. Try
  58. {
  59. If (parseInt (val)> = 18 & parseInt (val) <= 100)
  60. Return true;
  61. Return false;
  62. }
  63. Catch (err)
  64. {
  65. Return false;
  66. }
  67. },
  68. AgeText: 'Age input incorrect ',
  69. Alphanum: function (val, field)
  70. {
  71. Try
  72. {
  73. If (! // W/. test (val ))
  74. Return true;
  75. Return false;
  76. }
  77. Catch (e)
  78. {
  79. Return false;
  80. }
  81. },
  82. AlphanumText: 'enter English letters or numbers. Other characters are not allowed .',
  83. Url: function (val, field)
  84. {
  85. Try
  86. {
  87. If (/^ (http | https | ftp): // ([A-Z0-9] [A-Z0-9 _-] *) (/. [A-Z0-9] [A-Z0-9 _-] *) +) (:(/d + ))? //? /I. test (val ))
  88. Return true;
  89. Return false;
  90. }
  91. Catch (e)
  92. {
  93. Return false;
  94. }
  95. },
  96. UrlText: 'enter a valid URL address .',
  97. Max: function (val, field)
  98. {
  99. Try
  100. {
  101. If (parseFloat (val) <= parseFloat (field. max ))
  102. Return true;
  103. Return false;
  104. }
  105. Catch (e)
  106. {
  107. Return false;
  108. }
  109. },
  110. MaxText: 'exceeds the maxim ',
  111. Min: function (val, field)
  112. {
  113. Try
  114. {
  115. If (parseFloat (val)> = parseFloat (field. min ))
  116. Return true;
  117. Return false;
  118. }
  119. Catch (e)
  120. {
  121. Return false;
  122. }
  123. },
  124. MinText: 'smaller than minimal ',
  125. Datecn: function (val, field)
  126. {
  127. Try
  128. {
  129. Var regex =/^ (/d {4})-(/d {2})-(/d {2}) $ /;
  130. If (! Regex. test (val) return false;
  131. Var d = new Date (val. replace (regex, '$1/$2/$3 '));
  132. Return (parseInt (RegExp. $2, 10) = (1 + d. getMonth () & (parseInt (RegExp. $3, 10) = d. getDate () & (parseInt (RegExp. $1, 10) = d. getFullYear ());
  133. }
  134. Catch (e)
  135. {
  136. Return false;
  137. }
  138. },
  139. DatecnText: 'Use this date format: yyyy-mm-dd. For example: 2008-06-20 .',
  140. Integer: function (val, field)
  141. {
  142. Try
  143. {
  144. If (/^ [-+]? [/D] + $/. test (val ))
  145. Return true;
  146. Return false;
  147. }
  148. Catch (e)
  149. {
  150. Return false;
  151. }
  152. },
  153. IntegerText: 'Enter the correct integer ',
  154. Minlength: function (val, field)
  155. {
  156. Try
  157. {
  158. If (val. length> = parseInt (field. minlen ))
  159. Return true;
  160. Return false
  161. }
  162. Catch (e)
  163. {
  164. Return false;
  165. }
  166. },
  167. MinlengthText: 'too short ',
  168. Maxlength: function (val, field)
  169. {
  170. Try
  171. {
  172. If (val. length <= parseInt (field. maxlen ))
  173. Return true;
  174. Return false;
  175. }
  176. Catch (e)
  177. {
  178. Return false;
  179. }
  180. },
  181. MaxlengthText: 'too long ',
  182. Ip: function (val, field)
  183. {
  184. Try
  185. {
  186. If (/^ (? :(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) /.) {3 }(? : 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) $/. Test (val )))
  187. Return true;
  188. Return false;
  189. }
  190. Catch (e)
  191. {
  192. Return false;
  193. }
  194. },
  195. IpText: 'Enter the correct IP address ',
  196. Phone: function (val, field)
  197. {
  198. Try
  199. {
  200. If (/^ (0 [1-9] {3 })? (0 [12] [0-9])? [-])? /D {6, 8} $/. test (val ))
  201. Return true;
  202. Return false;
  203. }
  204. Catch (e)
  205. {
  206. Return false;
  207. }
  208. },
  209. PhoneText: 'Enter the correct phone number, for example, 0920-29392929 ',
  210. Mobilephone: function (val, field)
  211. {
  212. Try
  213. {
  214. If (/(^ 0? [1] [35] [0-9] {9} $)/. test (val ))
  215. Return true;
  216. Return false;
  217. }
  218. Catch (e)
  219. {
  220. Return false;
  221. }
  222. },
  223. MobilephoneText: 'Enter the correct mobile phone number ',
  224. Alpha: function (val, field)
  225. {
  226. Try
  227. {
  228. If (/^ [a-zA-Z] + $/. test (val ))
  229. Return true;
  230. Return false;
  231. }
  232. Catch (e)
  233. {
  234. Return false;
  235. }
  236. },
  237. AlphaText: 'Enter the English letter'
  238. });

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.